Basic PHP Programming Tutorial
November 11, 2007 by uttoransen
Filed under Tutorial
PHP is a great example of a server side language. Server side languages run on the server and then output to the visitor. This means that the visitors never see the real code of the PHP file, only the code generated by it.
This enables you to do many different things with PHP. The best of these is interactive scripts. This means that visitors can do things like log in and out of sites and you can create dynamic pages that can display different content, unlike static HTML pages that never change unless you change them.
PHP can be used to generate HTML code, so with this tutorial I am going to start out by teaching you how to output some simple text to the visitor.
Here is the code that you can use to output “Hello!” to the visitor:
php basic code
I will now run you through what this code does. The first bit says php and this basically tells the server that the code after that is PHP code, this is always needed when using PHP.
The next part says echo “Hello!”; and this is the line that prints Hello! to the user. The “echo” is the command used to tell the server to write something on the screen. The next bit is contained within quotes (” and then “) but you can also use ‘ and then ‘. Inside the quotes is what you want to print to the screen the last ” tells the server to stop printing.
Make sure you notice the ; after it. This bit is essential, it separates the PHP code so that the server understands that there is a new instruction coming.
Then there is “//This line says hello to the visitor”, the server doesn’t run that code, it is just a comment. Comments are useful as in more advanced scripts they can help you understand what something does. In PHP comments start at // and end when there is a new line. If you want multi-line comments then you can use /* to start the comment and */ to end the comment.
You should put this in a file and name it hello.php, or anything with the extension .php and then upload it to a PHP enabled hosting account/server and run. It should display a page saying “Hello!”.
You could accomplish this easily with HTML, now I will go on to teach you what PHP is useful for and what it can do but HTML cannot.
One thing PHP can do that HTML cannot is to store information. In PHP you get variables, in these variables you can store different types of information such as an integer (22), a string (burger) and many more. Variables are the core of making PHP dynamic and interactive.
They are mainly used to store information that is accessed many times throughout the program. If you have one variable that you use 10 times, if you change what is in the variable then it will change throughout the whole script. They can also be used to temporarily store data that a user has entered in a forum before it permanently stores them in a file or database.
another php code
Here is a very simple example of what variables can be used for:
This code starts of with the usual < and followed by ? and then “php” to tell the server the code is PHP. Then the new stuff starts.
In PHP variables do not have to be declared before they are used. This means you can just put values in a variable, in some languages you have to create the variable first, and then add data to it.
All variables in PHP start with a $ sign, with the $ then it will not be a variable. After that variables are allowed to have any letters or numbers or a combination of both. However it cannot start with the number.
There are other types of variables such as POST and REQUEST variables that start with $_ but you do not need to create these and are used in more advanced codes.
So the first variable is called $variable1. The number one is also being put into the variable using the = sign. If it is a number going into the variable you can just put = number but if you are putting in a string (eg. letters), you need to have ” “’s around the value (eg. $variable1 = “donkey”).
Then there is the ; at then end and then another line where the second variable is defined and data put into it.
Then there is the last variable, $answer which is going to contain the answer to the sum. In PHP you can add things together (also more such as *,/,-,< etc.) so this script does that. It is quite easy to understand, in this script the variable called $answer contains the contents of $variable1 added to the contents of $variable2. In this case $variable1 is 1 and $variable2 is 2 so $answer will be 3 (1+2).
Then there is an echo statement which you met earlier, this is printing the results of the equation. So it starts of with echo as usual and then says “The answer is ” which is normal. After that the quote ends and there is a space, dot then another space. In php, a dot (.) is used to concatenate, which means join this together. So in this case the dot is used to join “The answer is ” to the variable $answer1 (which contains 3) so it would be “The answer is 3″.
Note that variables do not need quotes around them when they are being printed in an echo statement.
Now you know some of the small basics of PHP, experiment with it, try out new things using -, * and / and in general just try and work things out. Try making a script where you have 5 variables. 4 of them have numbers in them. Start out by doing variable1 + variable2. Then divide the answer of that by variable3 and times the answer of that by variable4 and store the answer in variable5 and print that to the user.






