start PHP'ing

Write your "How To" regarding anything you are comfortable with and you feel it will help the forum members.

NOTE :: All threads started here will appear only after the approval from Administrator
Post Reply
mfrna
Posts: 57
Joined: Mon Jan 08, 2007 12:54 pm

start PHP'ing

Post by mfrna »

Hey everyone

don't know a lot about php ?!

ok , grab your pen, the class is beginning

I hate it when somebody starts a tutorial with a "Scientific background" ,so let's get to the point

Now what are the tools you need to make php scripts
1- a text editor (notepad would do if you are a pro) you can choose whatever program you like (I use little proggy called crimson editor) but make sure it saves the files as plain text,i.e. it doesn't color text or make it bold or italic .. etc

2- a host that supports php ,or you can setup your own server (download phptriad) if you have a slow connection.

3- a web browser (I guess you already have one!)

LET'S GO

first thing you need to know is that php scripts are not compiled, i.e. you don't have to compile your script before it's usable, all you need is a server that supports php.

if so, how does the server know that the script given is a php script?

when you open a php page in your browser (like the forum's main page for example) apache (the server program) opens the file (index.php in this case) and extracts html out of it untill it comes across this little sign (<?) or (<?php) or (<script language="php">)

here, it introduces the code to the php "processor" or "translator" that runs the code and give the results back as clear browser readable text (and hence human readable of course)

so,how can we mark a php code

php can be written as any form of the following

[PHP]<?
code
?>[/PHP]

or

[PHP]<?php
code
?>[/PHP]

or

[HTML]<script language="php">
code
</script>[/HTML]

often , you can use any form but sometimes server configuration can prohibit some form or can force another, so it's good to know them all.

in html language, the code is always greater than or equal to the resulting page text but that's not the case with php

want an example?!

here you go

1st proggy
open your editor,create an empty file and name it what ever you like (say mfrna.php) enter the following text and save your file in the htdocs folder (or any folder on your server)

[PHP]<?
phpinfo();
?>[/PHP]

[notice that phpinfo() is a function that prints out the php setup information]
[note :: the line ends with a semicolon,that's the case with all the php code lines unless stated

otherwise]

now, point your browser to http://localhost/mfrna.php
see how much data is printed on the screen from such a small code !!


now let's make a little site

would you like your website to look (/index.php?go=support) instead of (/support.html)

let's code it

2nd proggy

[PHP]<?
$go4it=$_GET['go'];
switch($go4it){
case "support":
require("./files/support.html");
break;
case "purchase":
require("./files/purchase.html");
break;
default:
require("./files/default.html");
}
?>[/PHP]

that's freaky, let's explain
1st line "<?" you know it
2nd line "$go=$_GET['go'];" here we defined a variable named go4it (ok now you know that variables are variable,can have values,and its name starts with a letter not a number or symbol,and capitalization matters i.e. Go is not equivalent to go nor gO) we gave it the value $_GET['go']

$_GET[] is a special php function that gets the values of variables off from forms or urls,the value go is called index and it's the variable that $_GET tries to get !!

3rd line "switch($go4it){" did you notice something .... yep, there's no semicolon there (write that note down)

what is switch .. switch is a php directive that takes the value of a variable and compares it with other values (can be other variables or just constants like our proggy here)

so what's up with the "{" thing?
ok , this is called a block, it opens a block of code and every thing after "{" is considered a part of the block until "}" is found

what is it useful for? here ,it marks all the "cases" of go4it variable

4th ,5th and 6th lines
[PHP] case "support":
require("./files/support.html");
break;[/PHP]

the first case of go4it variable,or the first value
this statement can be written in english as : if the value of go4it is equal to support then open the file "./files/support.html" and include it in our page's output and stop

4th line does the comparison part
5th line opens the support page (if and only if the comparison returned true)
6th line stops after printing the file support.html

the 7th,8th and 9th lines are the same but with a different case of course

so what if I drop the "break;" line??
well, if you open your browser on the support page ("/index.php?go=support") you'll get the support page .... followed by the purchase page .. and if you dropped the 9th line also you'll get the three pages altogether !!
this has benifits in some pages but not our little proggy here :)

back to the proggy
10th line down
[PHP] default:
require("./files/default.html");
}
?>[/PHP]

default is a special case, it's the result if no match happens between the variable and all the values of cases e.g. point you browser to "/index.php?go=fancypage" it'll just open our sweet beloved default.html page

this could be used for a 404 error page,you know.
12th line hmm,remember the "{" on third line ... yep that's the closure.
13th line tough one :D

ok folks,that's it for this tut
if you liked it let me know, I can cook up some more
if you have any questions .shoot

see ya around


SHAdmin
Posts: 2089
Joined: Sat Dec 18, 2004 11:28 am
Contact:

Post by SHAdmin »

This "How to" has been validated, and the author "mfrna" has been credited 25 points for it.
Contractjack
Posts: 74
Joined: Sat Dec 09, 2006 2:30 am

Post by Contractjack »

Thanks man thats a good "how to" Guide.
http://www.emailcash.com.au/join.asp?refer=L75414

register above to get free cash just buy doing surveys, real money, real people, no jokes.
elragal_30
Posts: 18
Joined: Fri Sep 15, 2006 5:46 pm

Post by elragal_30 »

thanks
it is very good and usefull topic
i wonder is this topic end or you will contineu???
i have some php books so can i post it or it not allowed???
SHAdmin
Posts: 2089
Joined: Sat Dec 18, 2004 11:28 am
Contact:

Post by SHAdmin »

The author of this topic is free to decide whether he wants to end this topic or if he wants to continue it by adding more posts as a continuation to this topic.

You are not allowed to post php books which are copyrighted or which have been created from copyrighted hard copies. If you have written that book on your own, you are free to post it, otherwise you must have the written permission from the author for posting it here.
mfrna
Posts: 57
Joined: Mon Jan 08, 2007 12:54 pm

Post by mfrna »

I'd continue writing this stuff whenver I have spare time :)
TheCrymsonLegends
Posts: 1246
Joined: Wed Feb 16, 2005 6:59 am

Post by TheCrymsonLegends »

It seems like many of these how to's could be seen as a Copyright infringement but everyone can always re-write any book to make it easier to understand and change some simple code names to make it there own. The whole go4it bit was part of a How To I posted but it was completely different which was proclaimed to be copywritten by someone but it's all code which has already been figured out by many others so it's perfectly fine to write your own how to for any code which you want to help others learn.

This is a great How-To which is wonderful to make your own easy access Menu Bar for a portal system, it allows you to post a simple code on the rest of your pages to do the same thing as anything else.

Great work on this how-to and if anyone says anything about a copyright infringement you wrote it out yourself so don't worry about it.

And to the other guy who wanted to know wether or not he could, as long as it's a book you wrote yourself there isn't ever anything wrong with it, because then it becomes your book on how to do something. So I hope you went for it.

Good work on all these how-to's it's nice to see people are giving advice for others to use.
Reached 5000 Credits! The highest of any member on Smokyhosts! New milestone for Me!
4ran

Post by 4ran »

thank you for sharing ! this was really useful ;)
Stam
Posts: 127
Joined: Thu Mar 09, 2006 5:24 pm

Post by Stam »

Simply explained perfect!
SHAdmin
Posts: 2089
Joined: Sat Dec 18, 2004 11:28 am
Contact:

Post by SHAdmin »

TheCrymsonLegends wrote:It seems like many of these how to's could be seen as a Copyright infringement but everyone can always re-write any book to make it easier to understand and change some simple code names to make it there own. The whole go4it bit was part of a How To I posted but it was completely different which was proclaimed to be copywritten by someone but it's all code which has already been figured out by many others so it's perfectly fine to write your own how to for any code which you want to help others learn.

This is a great How-To which is wonderful to make your own easy access Menu Bar for a portal system, it allows you to post a simple code on the rest of your pages to do the same thing as anything else.

Great work on this how-to and if anyone says anything about a copyright infringement you wrote it out yourself so don't worry about it.

And to the other guy who wanted to know wether or not he could, as long as it's a book you wrote yourself there isn't ever anything wrong with it, because then it becomes your book on how to do something. So I hope you went for it.

Good work on all these how-to's it's nice to see people are giving advice for others to use.
Very well said there
Post Reply