This Tutorial was made by Zenith for beginners to have a better understanding in PHP. It was well explained and helpful for those who are just starting to learn PHP.
Note: Watching videos and playing around with PHP code was the most advisable to learn it faster.
----
Installing under Windows
Instructions can be found at : You do not have permission to view the full content of this post. Log in or register now.
Another possibility is to download and run PHPTriad from You do not have permission to view the full content of this post. Log in or register now.. This installs the very popular triad of programs: PHP, MySql and Apache. It should be very cool and very easy.
As in all good tutorials, we start with the (in)famous HelloWorld example. Here's the code:
You should save this piece of code in a file with the extension .php. If that doesn't work, then try other extensions like .php3 or .phtml. If you can't figure out the right extension, then ask whoever installed the webserver. If that's you, then have a look at the file called httpd.conf - if you're using Apache that is...
It should contain a line that looks like this:
Anyway --- this line informs Apache that files ending with .php are PHP scripts.
Back to the example, which is somewhat silly. But it can be used to teach you a few things anyway.
Most of the code is just plain HTML. Only the part between <?php and ?> is PHP-code. You use either <?php or just <? to start the code and ?> to stop it again. The parser works within these two tags. Between the <?php and ?> you put a number of statements, each of which must be ended by a ; (a semi-colon).
Between the two tags there is a comment and one statement, echo "Hello World";. The comment will be completely ignored, and the statement just prints the string "Hello World" into the webpage, which is then sent to the browser. After the parser has processed the page, the final output that is sent to the browser looks this way:
As you can see, there's no trace of the PHP-code left in the final output. In the following code-snippets I won't include all the HTML code, but remember that you'll have to use HTML if you want your PHP scripts to work on the net. You'll also have to include the right HTML codes in the strings you output from PHP, if you're outputting more than just very simple strings.
You should think of a PHP-script as a normal web page written in HTML, since most of the page is still just plain-old HTML. But between the start- and end-tags (<?php or <? and ?>) the PHP-parsed spices the page up with possibly dynamic content.
To use a variable you have to assign a value to it. It is done this way:
The name of the variable is on the left of the = sign and the new value of the variable is on the right. Here we assign the value of 18 to the variable $age: $age = 18;
There are several different types of variables. You have already met the integer and string types. Lets start with the integer type.
Numbers
Dealing with numbers is easy with PHP. You can just use them as you like. All the normal rules about precedence apply, so you can write something like this:
As you can see, everything works as you would expect. Any variables are automatically substituted with their values. This is done before the new value is stored in the variable on the left side of the =-sign. This means that you can do something like this:
Strings
To assign a string to a variable, you must use quotes. You can choose between single quotes (') or double quotes ("). The kind of quote you should choose depends on the string you work with in a given situation. There are some differences between the two types of quotes, which the following code should demonstrate:
This code produces the following output:
Hello, my last name is $last_name.
"Why was it only the first string that worked properly?", you might ask. The answer is that when using double quotes, PHP performs what is known as variable-expansion.
That means that PHP expands (substitutes) $first_name with it's value, just as you saw it when using numbers. The result is that the string stored in $greeting1 is "Hello, my name is Martin." (the quotes are not part of the string stored in $greeting1). When assigning to $greeting2 using single quotes, PHP doesn't substitute any variables and $greeting2 ends up with "Hello, my last name is
But there's more magic with the double quotes. They will also expand other special characters, such as newline-charecters (\n). This mean that the following string:
You've met the echo statement before (in the Hello World example) and it just outputs the string after it to the webbrowser. As I said earlier there's no trace of the PHP code, but notice that there's a linebreak between the two words. (The linebreak can only be seen in the source of the page, because the webbrowser treats the linebreak as a normal space.) You should also notice that there's no space before or after the \n-character. Because you use the \-character to indicate that the next character is special, you have to write \\ when you want to have a single \. The backslash (\) is called an escape-character, since it makes the characters escape from their normal role. The escape-character also has to be escaped.
If you want to have a $-charactar in a string enclosed in double-quotes, you'll need to escape it, or else PHP will look for a variable, since all variables start with a $.
This string will give you a little problem:
If you try it out, you'll get an error. That's because the parser expects the string to stop after the second double quote, just before the word gimpster. The rest of the line is just garbage to the parser so it complains loud. Perhaps it would help if we escaped the double quote? Exactly! The following string is correct:
The same applies if we had been using single quotes throughout the string. But there's another way to solve the problem. You could quote the string with single quotes and then use as many double quotes within the string as you would like. Or you could do the inverse, and enclose the string in double quotes and use single quotes in the string. This solves a problem you would have when outputting HTML-code with lots of double quotes:
If we had quoted the string with double quotes, we would have had to escape all the other double quotes. But on the other hand, in the string above PHP wouldn't do any variable-substitution. Also, the escape-characters will not be understood, so you can't write \n in a string enclosed by single quotes. Instead you can put the newlines directly into the code, as I've done in the example. The final output will still contain those newlines.
It's only when you're evaluating expressions (such as in assignments and echo statements) that the difference between single and double quotes comes into play. After the assignment is done, you wont be able to tell how the string was produced. Consider this code:
The two variables $var1 and $var2 will both contain the string Martin Geisler after the evaluation, without any sign of all the different kinds of quotes that were used in the construction and PHP will not make any distinction between the values stored in the two variables.
If you want to concatenate (add together) two strings you use the .-character (a dot or full stop) like this:
Now $full_name will contain the string "MartinGeisler". That's probably not what you wanted, it would be better with a space between the two words. To do this, you execute the following code: $full_name = $first_name . ' ' . $last_name;
Notice that the space between the variable-name and the dot is ignored --- it's the string with the space that's important. You use the dot (full-stop) each time you have two strings that you need to put after one another.
But you've also seen how this could be solved using variable-substitution. This is often easier to read, just remember to use double-quotes around the string. So this code gives the same result:
When using variable substitution you might run into a little problem. The problem occurs when you want to substitute a variable in a string and have text immediately after the variable. You have to delimit the name of the variable somehow. Using curly braces is the solution as shown in this code:
Here we're playing with English words in plural form. The first occurrence of $noun is substituted correctly, even when it's followed immediately by a comma because you cannot have a comma in the name of a variable. But you could certainly have a variable called $nouns, so we have to use curly braces to indicate that we want the variable called $noun and not some nonexistent variable called $nouns.
Arrays
Arrays are a single variable that can hold a number of different values at the same time. To access the different values you specify them by using brackets after the name. It works like this:
(There's no syntax hightlighting in the above example because it involves square brackets with numbers in them, which is also used for footnotes in PhpWiki. So some of the following examples will be in plain black & white.)
Now we have made an array, and filled in the two first slots. Notice that the numbering starts at zero and not one. We can recall them like this:
If you don't care which slot you get when you assign to an array, then use a pair of empty brackets after the name:
This filled the first three slots of the array, numbered 0, 1, and 2. When you want to retrieve the information again, you must specify which slot you want access to. But you can also identify the different slots by name. This makes everything much more readable and easy to understand:
Notice how I broke the last line to make it shorter and how I used the dot operator to glue the strings together with the variables. The parser will ignore all whitespace, and the result is:
Hello Martin! We know that you are 18 years old.
Also note how I split the string up into its individual pieces. This is often the easiest way to deal with associative arrays. Another possibility is to use variable substitution with curly braces like this:
This code gives the same result. You can put complex expressions inside such a set of curly braces. This includes accessing elements in multidimensional arrays and object properties.
Please do use descriptive names for the slots in your arrays. It makes the code much more clear, and helps to indicate what the different slots are intended for.
Note: Watching videos and playing around with PHP code was the most advisable to learn it faster.
----
Installing under Windows
Instructions can be found at : You do not have permission to view the full content of this post. Log in or register now.
Another possibility is to download and run PHPTriad from You do not have permission to view the full content of this post. Log in or register now.. This installs the very popular triad of programs: PHP, MySql and Apache. It should be very cool and very easy.
As in all good tutorials, we start with the (in)famous HelloWorld example. Here's the code:
PHP:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>
<?php
// This is a comment that extends to the end of the line.
/* This is multi-line comment. Comments are ignored by PHP,
 but provide valuable information to people reading the source. */
echo "Hello World!\n";
?>
</p>
</body>
</html>
You should save this piece of code in a file with the extension .php. If that doesn't work, then try other extensions like .php3 or .phtml. If you can't figure out the right extension, then ask whoever installed the webserver. If that's you, then have a look at the file called httpd.conf - if you're using Apache that is...
It should contain a line that looks like this:
AddType application/x-httpd-php .php
Anyway --- this line informs Apache that files ending with .php are PHP scripts.
Back to the example, which is somewhat silly. But it can be used to teach you a few things anyway.
Most of the code is just plain HTML. Only the part between <?php and ?> is PHP-code. You use either <?php or just <? to start the code and ?> to stop it again. The parser works within these two tags. Between the <?php and ?> you put a number of statements, each of which must be ended by a ; (a semi-colon).
Between the two tags there is a comment and one statement, echo "Hello World";. The comment will be completely ignored, and the statement just prints the string "Hello World" into the webpage, which is then sent to the browser. After the parser has processed the page, the final output that is sent to the browser looks this way:
PHP:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>
Hello World
</p>
</body>
As you can see, there's no trace of the PHP-code left in the final output. In the following code-snippets I won't include all the HTML code, but remember that you'll have to use HTML if you want your PHP scripts to work on the net. You'll also have to include the right HTML codes in the strings you output from PHP, if you're outputting more than just very simple strings.
You should think of a PHP-script as a normal web page written in HTML, since most of the page is still just plain-old HTML. But between the start- and end-tags (<?php or <? and ?>) the PHP-parsed spices the page up with possibly dynamic content.
Before I show you any more examples I had better introduce to you the different kinds of variables we have in PHP. Variables are what you use to store something for later use. You might want to refer to your age, so it would be nice to define the age only once, and then just refer to it later. When your age changes, you just update the definition of your age, and then all other calculations that refer to the age are also updated. All the names of the variables must start with the $ character. After that there must be a letter. The rest of the name can consist of both letters and numbers. Here are three examples of valid names for a variable: $name, $address2 and $colour_30. As you can see, you can also use the _ (underscore) character in your names. It is often used as a replacement for spaces, since they are not allowed.
To use a variable you have to assign a value to it. It is done this way:
PHP:
$name = 'Martin';
The name of the variable is on the left of the = sign and the new value of the variable is on the right. Here we assign the value of 18 to the variable $age: $age = 18;
There are several different types of variables. You have already met the integer and string types. Lets start with the integer type.
Numbers
Dealing with numbers is easy with PHP. You can just use them as you like. All the normal rules about precedence apply, so you can write something like this:
PHP:
$a = 4;
$b = 7;
$c = 2 + 3 * $a + 5 * $b // This is evaluated as 2 + (3 * 4) + (5 * 7) = 49
As you can see, everything works as you would expect. Any variables are automatically substituted with their values. This is done before the new value is stored in the variable on the left side of the =-sign. This means that you can do something like this:
PHP:
$a = 5;
$b = 10;
$a = (2 * $a + 5)/$b; // Evaluated as $a = (2 * 5 + 5)/10 = 1.5
Strings
To assign a string to a variable, you must use quotes. You can choose between single quotes (') or double quotes ("). The kind of quote you should choose depends on the string you work with in a given situation. There are some differences between the two types of quotes, which the following code should demonstrate:
PHP:
$first_name = 'Martin';
$greeting1 Â = "Hello, my first name is $first_name.";
echo $greeting1;
$last_name  = "Geisler";
$greeting2 Â = 'Hello, my last name is $last_name.';
echo $greeting2;
This code produces the following output:
Hello, my first name is Martin.
Hello, my last name is $last_name.
"Why was it only the first string that worked properly?", you might ask. The answer is that when using double quotes, PHP performs what is known as variable-expansion.
That means that PHP expands (substitutes) $first_name with it's value, just as you saw it when using numbers. The result is that the string stored in $greeting1 is "Hello, my name is Martin." (the quotes are not part of the string stored in $greeting1). When assigning to $greeting2 using single quotes, PHP doesn't substitute any variables and $greeting2 ends up with "Hello, my last name is
PHP:
$last_name.".
But there's more magic with the double quotes. They will also expand other special characters, such as newline-charecters (\n). This mean that the following string:
PHP:
echo "Martin\nGeisler";
will look like this in the source of the page:
Martin
Geisler
You've met the echo statement before (in the Hello World example) and it just outputs the string after it to the webbrowser. As I said earlier there's no trace of the PHP code, but notice that there's a linebreak between the two words. (The linebreak can only be seen in the source of the page, because the webbrowser treats the linebreak as a normal space.) You should also notice that there's no space before or after the \n-character. Because you use the \-character to indicate that the next character is special, you have to write \\ when you want to have a single \. The backslash (\) is called an escape-character, since it makes the characters escape from their normal role. The escape-character also has to be escaped.
If you want to have a $-charactar in a string enclosed in double-quotes, you'll need to escape it, or else PHP will look for a variable, since all variables start with a $.
This string will give you a little problem:
PHP:
$name = "Martin "gimpster" Geisler";
If you try it out, you'll get an error. That's because the parser expects the string to stop after the second double quote, just before the word gimpster. The rest of the line is just garbage to the parser so it complains loud. Perhaps it would help if we escaped the double quote? Exactly! The following string is correct:
PHP:
$name = "Martin \"gimpster\" Geisler";
The same applies if we had been using single quotes throughout the string. But there's another way to solve the problem. You could quote the string with single quotes and then use as many double quotes within the string as you would like. Or you could do the inverse, and enclose the string in double quotes and use single quotes in the string. This solves a problem you would have when outputting HTML-code with lots of double quotes:
PHP:
echo '
<a href="mailto:gimpster@gimpster.com">
 <img src="image.gif" width="16" height="16" alt="me" border="0">
</a>';
If we had quoted the string with double quotes, we would have had to escape all the other double quotes. But on the other hand, in the string above PHP wouldn't do any variable-substitution. Also, the escape-characters will not be understood, so you can't write \n in a string enclosed by single quotes. Instead you can put the newlines directly into the code, as I've done in the example. The final output will still contain those newlines.
It's only when you're evaluating expressions (such as in assignments and echo statements) that the difference between single and double quotes comes into play. After the assignment is done, you wont be able to tell how the string was produced. Consider this code:
PHP:
$first_name = 'Martin';
$last_name = "Geisler";
$var1 = "$first_name $last_name";
$var2 = 'Martin Geisler';
The two variables $var1 and $var2 will both contain the string Martin Geisler after the evaluation, without any sign of all the different kinds of quotes that were used in the construction and PHP will not make any distinction between the values stored in the two variables.
If you want to concatenate (add together) two strings you use the .-character (a dot or full stop) like this:
PHP:
$first_name = 'Martin';
$last_name = 'Geisler';
$full_name = $first_name.$last_name;
Now $full_name will contain the string "MartinGeisler". That's probably not what you wanted, it would be better with a space between the two words. To do this, you execute the following code: $full_name = $first_name . ' ' . $last_name;
Notice that the space between the variable-name and the dot is ignored --- it's the string with the space that's important. You use the dot (full-stop) each time you have two strings that you need to put after one another.
But you've also seen how this could be solved using variable-substitution. This is often easier to read, just remember to use double-quotes around the string. So this code gives the same result:
PHP:
$full_name = "$first_name $last_name";
When using variable substitution you might run into a little problem. The problem occurs when you want to substitute a variable in a string and have text immediately after the variable. You have to delimit the name of the variable somehow. Using curly braces is the solution as shown in this code:
PHP:
$noun = 'car';
echo "A $noun, two ${noun}s";
Here we're playing with English words in plural form. The first occurrence of $noun is substituted correctly, even when it's followed immediately by a comma because you cannot have a comma in the name of a variable. But you could certainly have a variable called $nouns, so we have to use curly braces to indicate that we want the variable called $noun and not some nonexistent variable called $nouns.
Arrays
Arrays are a single variable that can hold a number of different values at the same time. To access the different values you specify them by using brackets after the name. It works like this:
PHP:
$name[0] = 'Martin';
$name[1] = 'Geisler';
(There's no syntax hightlighting in the above example because it involves square brackets with numbers in them, which is also used for footnotes in PhpWiki. So some of the following examples will be in plain black & white.)
Now we have made an array, and filled in the two first slots. Notice that the numbering starts at zero and not one. We can recall them like this:
PHP:
echo $name[0] . ' ' . $name[1];
If you don't care which slot you get when you assign to an array, then use a pair of empty brackets after the name:
PHP:
$name[] = 'Martin';
$name[] = 'gimpster';
$name[] = 'Geisler';
This filled the first three slots of the array, numbered 0, 1, and 2. When you want to retrieve the information again, you must specify which slot you want access to. But you can also identify the different slots by name. This makes everything much more readable and easy to understand:
PHP:
$personal_data['first name'] = 'Martin';
$personal_data['nickname'] = 'gimpster';
$personal_data['last name'] = 'Geisler';
$personal_data['age'] = 18;
echo 'Hello ' . $personal_data['first name'] .
  '! We know that you are ' . $personal_data['age'] .
  ' years old.';
Notice how I broke the last line to make it shorter and how I used the dot operator to glue the strings together with the variables. The parser will ignore all whitespace, and the result is:
Hello Martin! We know that you are 18 years old.
Also note how I split the string up into its individual pieces. This is often the easiest way to deal with associative arrays. Another possibility is to use variable substitution with curly braces like this:
PHP:
echo "Hello {$personal_data['first name']}! " .
  "We know that you are {$personal_data['age']} years old.";
This code gives the same result. You can put complex expressions inside such a set of curly braces. This includes accessing elements in multidimensional arrays and object properties.
Please do use descriptive names for the slots in your arrays. It makes the code much more clear, and helps to indicate what the different slots are intended for.