Hey there! Its Ferg here, you might remember me from such articles as HTML in submissions or How To Sell Your Games. If not, who cares! You don't care about me, you only care about this article so continue reading.

There's just so many of you who want to be able to make the next DC. Soon, so many of you will be able to make the next DC. If you get stuck ask. In these tutorials I am not going to tell you EVERYTHING you can do in PHP, I'm going to tell you the parts that are useful to make a site like this or mine http://www.fergs.co.uk

Just remember that for this to work you need a PHP enabled server. If you don't have one then you can just ask to be hosted by me. If you ask nicely I may host you

In this first part of my few PHP tutorials I am going to give you a few basic pieces of info you need to know before we dig into PHP.

Open your favourite text editor, for example Notepad. Type the following in your text editor:

<?php

echo "Hello, World!";

?>


The first line <?php starts the PHP code. The second line echo "Hello, World!"; prints the words Hello, World! to the screen. DO NOT forget the ; at the end of this line. The third line ?> ends the PHP code. What you have learned here is how to start a PHP document, how to print something to the screen using echo ""; with the text you want to print being between the quotes and you have learned how to end a PHP document.

Save this document as hello.php. Make sure when you save it the 'Save As Type' is set to 'All Files'. All webpages that contain php code must b saved with the .php extension. E.g. hello.php, firstdoc.php etc. Upload it to your server and visit it in your browser.

Variables

Variables are an extremely important part of PHP. Open up hello.php and replace what is there with this:

<?php

$string = "Hello, World!";

echo $string;

?>


You know that the first line starts the PHP code. The second line is new. The $string is a variable. Notice the $ before the word string. Variables can have any names you want them to have, e.g. instead of $string you could have had $iloveklik. It doesn't matter. The part that is on the right of the = sign is the value of the variable $string. In this case $string is assigned the value of a string of characters. If you want to assign a string of characters to a variable you must enclose the characters within quotes, e.g. $mystring = "My first string"; or $anothervariable = "Another string";
The third line prints the variable $string to the screen. The fourth line ends the PHP code.

Other Examples Of Variables And Mathematical Operators

You can also assign numbers to a variable like so:

$number = 34;

Notice that there are no quotes around the number. This makes the variable $number have the value 34. This is the number value 34 and not the string value 34. The difference is that if I were to subtract one from this variable it would equal 33 whereas if I had:

$number = "34";

This would just be a string of characters that could only be printed to the screen. You cannot subtract one from the variable above.

You can do mathematical calculations using variables too. For example, type the following in your text editor:

<?php

$value1 = 3;
$value2 = 6;
$value3 = $value1 + $value2;

echo $value3;

?>


Line one starts the PHP code. Line two assigns the value 3 to the variable $value1. The third line assigns the value 6 to the variable $value2. Line four adds $value1 and $value2 and assigns the result to the variable $value3. Line 5 then prints the value of $value3 to the screen. Line 6 ends the PHP code. So the above is the same as saying $value3 = 3 + 6; which equals 9. This value is then printed to the screen so you should see the number 9 in your browser after you save the file and upload it to your server.

You can also subtract, divide and multiply. Here are examples of each of these:

$value1 = 4;
$value2 = 2;

$subtract = $value1 - $value2;
$divide = $value1 / $value2;
$multiply = $value1 * $value2;


You can also do larger calculations using brackets. Whatever you put in brackets in mathematics the brackets are always calculated first. E.g.

$value1 = 4;
$value2 = 2;

$sum = (4 + $value2) * $value1;


The variable $sum is like saying the following (4 + 2) x 4. This equals 6 x 4 = 24. Try printing the variable $sum to the screen using echo "$sum";. You should see the value 24.

One last piece of information for the first part of my PHP tutorials. Say at the start of a PHP webpage you assign the variable $myvariable the value of 3:

$myvariable = 3;

Then say you used the same variable name later in the document and gave it the value 4:

$myvariable = 4;

This variable will now have the value 4 and not the value 3.

Today you learned the basic of basic PHP. In Part 2 you will learn about if conditionals, else, elseif, switch conditional, while loops and for loops. See you soon!

Ferg :: Visit my site at http://www.fergs.co.uk