Important alert: (current site time 11/26/2014 1:04:45 PM EDT)
 

article

Basics of PHP

Email
Submitted on: 9/15/2002 10:11:59 AM
By: John Croucher  
Level: Beginner
User Rating: By 15 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 35092
author picture
 
     This tutorial provides any beginner with the basic skills required to start programming in PHP.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

PHP is a server side scripting language that makes it easy to create dynamic websites.
The code is similar to Java and C++ so if you know one or both of these languages you should be able to pick up PHP fairly easily.

To use PHP you must either upload your site to a server that supports PHP or set up your own server. Unless you have a fast internet connection uploading your files for testing all the time will most likely not be a very good option for you. A good option is to have your own server on your local workstation. The best way to do this would be to use Windows 2000 or XP with Internet Information Services installed. You can then download the PHP engine and install it on your computer. This will allow you to test your pages quickly. However there are other ways of doing this such as the Apache sever.

PHP is an embedded language. This means that you can embed code within a HTML document. There are a few ways in which this can be done:

<? echo ”Test Message”; ?>

This is the most common way of embedding code, but it may cause problems if your code must co-exist with XML.

Another common way to embed is:

<?PHP echo ”Test Message”; ?>

I recommend using this method for embedding for compatibility with other systems.

There are two other alternate methods but these two aren’t used very often:

<% echo ”Test Message”; %>

By default this method is turned off, and I do not recommend using it.

The final way is similar to putting JavaScript in your page:

<SCRIPT LANGUAGE=”php”>
     echo ”TestMessage”;
</SCRIPT>

Every file you create that contains PHP code you must make sure the extension is the PHP extension set on your server. I usually just use .php, but some people say that you should use the PHP version .php3 or .php4. I have never had any problems but there may be some issues with other servers which is why I still mentioned it.

As I mentioned earlier PHP’s syntax comes from languages like C++. This means that you can use: // /* */ and even # for comments. Also the structure of things like for loops are similar, but variables in PHP are different.
All variables have a $ sign at the start and is followed by an alphabetic or underscore character. No data type declarations are necessary and you do not have to initialise the variables:

<?
$name;
$name=”John”;
echo “My name is $name”;
?>

OUTPUT:
     My name is John

Notice how I can just put the variable name inside the output string.
Double quotes will be checked for variables and escape sequences.
If you do not want this to happen you would use single quotes:

<?
$name;
$name=”John”;
echo ‘My name is $name’;
?>

OUTPUT:
     My name is $name

If you do wish to specify the data type for a variable you can use type casting:

$number = (int) = “342asd”;

This will create an integer value of 342.
The types PHP supports are as follows:

(int), (integer)                  – Cast to integer
(real), (double), (float)     – Cast to float
(string)                             – Cast to string
(array)                              - Cast to array
(object)                             - Cast to object


Selection is something that is very important to know for every language. Selection in PHP is very similar to C++ and Java. We will fist start with the If statement:

if(expression){
     statements
}

This is a basic If statement. It is a bit hard to understand this way because we have nothing to test for so I will expand and put some sample data in.

if($name == “John”){
     echo “John is the creator of this tut”;
}

Now with this data in it will say if the value in $name is equal to the string “John” then do the code within the braces.
We can then further expand on this to do something else if the value is not true:

if($name == “John”) {
     echo “John is the creator of this tut”;
} else {
     echo “$name is not the creator of this tut”;
}

This code will do the same as previous but it will now output the second echo statement if it is not true.
Say the value of $name is “Bob”. It will go through and say is $name the same as “John”. No its not so we will go to else and output “Bob is not the creator of this tut”.
Getting confused? I hope not because we are going to expand on this code again and test for a second name.

if($name == “John”){
     echo “John is the creator of this tut”;
} else if($name == “Bob”){
     echo “I don’t know any Bob”;
} else {
     echo “$name is not the creator of this tut”;
}

This code again does the same as previous but it will test for two conditions.
Say the value of $name is still “Bob”. It will look at the first condition. Is the value in $name the same as “John”? No, So go to the next if. Is the value in $name the same as “Bob”? Yes, output “I don’t know any Bob”.
You can keep expanding on this testing for as many conditions as you wish.

Just a note if you have only one line after you if statement you don’t have to have braces:

if($name == “John”)
     echo “John is the creator of this tut”;

But if you want two or more statements you must have braces. I just find it easier to use braces all the time.

Also you may be wondering why I am using “==”. Well if you think to where you assign a value:

$name = “John”;

That is how you assign a value, so if you had:

if($name = “John”)

You would be trying to assign a value and you would receive an error.
Other comparisons you can do are things like &&, ||, >, <
I will explain the Boolean operators in a later tutorial.

The next selection statement is the Switch. The basic syntax is as follows:

switch(expression) {
case expression:
     statements
     break;
default:
     statements
     break;
}

Again this is a bit hard to follow so I will put some meaningful code in to help:

switch($name) {
case “John”:
     echo “John is the creator of this tut”
     break;
default:
     statements
     break;
}

This will basically do the same sort of thing as an if statement. If the value in $name is “John” then output “John is the creator of this tut”. If not then go to the next case, in this example the next case is the “default:”. This acts in a similar way to “else” in an if statement.
Also just like if statements you can expand and test for more values. To do this all you need to do is to add in more case statements.

switch($name) {
case “John”:
     echo “John is the creator of this tut”
     break;
case “Bob”:
     echo “I don’t know Bob”
     break;
default:
     statements
     break;
}


That concludes my first PHP Tutorial. Check out my site for more tutorials and code.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/16/2002 5:08:03 AMTrueLyFE

I already know PHP, but from a newbz mindset, this doesn't quite work. Why?

You just jump into code.

Try to explain the BACKGROUND to the language, as well as how the language thinks - now how the code thinks.

For those knowing other languages such as Java or C++, it is a decent pointer to a couplea key aspects to the language.
(If this comment was disrespectful, please report it.)

 
9/17/2002 11:13:55 AM

What's the advantage/disadvantage of PHP vs ASP pages with VBScript?
Is PHP more of an open-source Linux thing?
(If this comment was disrespectful, please report it.)

 
9/18/2002 12:08:02 AMJohn Croucher

PHP is more of a Linux language but it is on more Windows servers now. But I think PHP and ASP both have advantages. I think it just comes down to what your system will support, what you want the pages to be able to do and also which one you feel more comfortable using.
(If this comment was disrespectful, please report it.)

 
9/18/2002 12:30:17 AMMehdi Hanbali

perl rules, php and asp are OK, but perl still rules :)
(If this comment was disrespectful, please report it.)

 
9/19/2002 7:39:30 PMTrueLyFE

I tried to code in perl but I just didn't get it. Mehdi, try using PHP. If you are at all in the slightest familiar with C, then PHP will be really quick to grasp. :D
(If this comment was disrespectful, please report it.)

 
2/20/2003 8:07:55 AMKevin Figg

Personally I like this. I'm fluent in ASP and VB, and also Pascal/Delphi. This is just what I needed.

Thanks!
(If this comment was disrespectful, please report it.)

 
3/2/2003 3:59:36 PM

I'm a newb in PHP, but i fully understood what you where talking about! Nice job I learned alot a didn't missed a thing!
(If this comment was disrespectful, please report it.)

 
4/23/2003 3:51:58 AMhungheykwun

wats it used for?
(If this comment was disrespectful, please report it.)

 
4/23/2003 11:44:02 AMJohn Croucher

This is a tutorial on the basics of PHP. It is used for learning how to programm in PHP.
(If this comment was disrespectful, please report it.)

 
7/6/2003 11:07:30 PM

i'm an absolute newb to most languages aside from HTML and Javascript, this is really helpful!! I learnt a lot. Thank Mr. Croucher!!
(If this comment was disrespectful, please report it.)

 
12/6/2003 2:36:24 AM

OMG THANK YOU! This is exactly what ive been looking for as opposed to something that explains everything the intellect's way, and this just jumps into what i WANT to know *kisses your feet*
(If this comment was disrespectful, please report it.)

 
2/17/2004 11:45:42 PMCor!

I don't know which tutorial the first poster was reading, but anyway I only know VB and no C++ based languages, yet this tutorial was perfect and simple. Well done. Now waiting for lesson II :-) 5 annodised green ball bearings.
(If this comment was disrespectful, please report it.)

 
3/7/2005 1:20:54 PMTheAlas

Great stuff, very "patient" tut :).

As c++ guru I find php easy, you forgot to mention that most standard functions from c headers can be used as well (file io for example).

5 weird green spherical objects from me.
(If this comment was disrespectful, please report it.)

 
7/1/2005 12:06:11 AMTrevor K.

This is just what I have been looking for. I only know VB and this is very different, but this helps heeps. 5 magical golf balls. Let me know if you make another tutorial.
(If this comment was disrespectful, please report it.)

 
7/9/2005 5:13:41 PMBmaq007

Dunno, am already good with PHP. I think this covers all those REAL PHP parts and skips the parts that nearly bored me to death. 5 (u know whoz balls) balls to ya. PEACE FROM DA MAQ!

(If this comment was disrespectful, please report it.)

 
9/18/2005 2:23:20 PMshah Ji

I am an advanced Java C++ programmer...and learning PHP this tutorial have given me the start up that I needed nice work and keep it up.... ***** from me :)
(If this comment was disrespectful, please report it.)

 
12/19/2005 7:45:37 AMIvo Smits

Note that PHP may try to initialize a variable with query string, post or cookie data, depending on your settings. If you specify query string "name=me" it may set $name to 'me'. It can be very hard to find out why your variable isn't empty.
(If this comment was disrespectful, please report it.)

 
12/23/2005 2:53:26 PMBart Goossens

Good for a beginner :
look at apress for the free book
A programmer's Introduction ror PHP 4.0 (W. J. Gilmore)
(If this comment was disrespectful, please report it.)

 
2/20/2007 9:10:00 PMMubashir

hi,
today i m start PHP so plz guide me to install it properly if u can...i will be thank full 2 u in this regard... plz
(If this comment was disrespectful, please report it.)

 
3/10/2007 8:18:47 AM


(If this comment was disrespectful, please report it.)

 
2/5/2008 2:59:07 AM

Well I'm not a total PHP newbie but I have been learning of things that I didn't know so I wanted to do some reading. Your tutorial was very helpful. Yours and a few others I have been reading. And I just wanted to say that I really appreciate you writing and publishing things like this for people like me. :)
(If this comment was disrespectful, please report it.)

 
6/25/2008 3:50:52 AMbobie

what's that? no demonstration
(If this comment was disrespectful, please report it.)

 
12/23/2010 12:59:29 PMmasum

Thank you for your articles
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.