Basics of PHP
What is PHP?
PHP is a server side language that is used for making your web site more dynamic and adding features.
How do i use it?
This tutorial will show you how to use the basic functions of php. The best way i feel, to learn PHP is by practice and following examples. First of all you will need either PHP installed on your computer or a web server that has PHP installed on it. To test if your web server has PHP installed make a new file in notepad, or a text editor of your choice. In the file, insert this line.
Code:
<?php phpinfo(); ?>
Save the file as any name you desire with the extension .php and then upload to your web server. Open the URL of this file and see if it has worked. If it has worked then you will see lots of information about the PHP version your web server has installed. If you just see the code you entered in plain text format, PHP is not enabled, so you need to find some web space that allows PHP or install it on your machine. You can find the download instructions at www.php.net.
Hello World!
NOTE: All php code must be inserted between the 2 php tags "<?php" to open and "?>" to close. Any php code inserted out of these tags will not be executed.
Now you have your php working i will show you how to print text onto the page using PHP.
Make a new file in your editor and type the following.
Code:
<?php echo "Hello World"; ?>
This piece of code will print the words Hello World into the web page. This is done by using the echo command and enclosing the text to be printed in quotes ("). Alternatively print can be used instead of echo, both work fine so it doesn't matter which you use, they do the same thing. At the end of each PHP statement you will need to put a semicolon(;), else PHP will produce an error.
Save your file as hello.php and upload. You should see Hello World. Amazing isn't it :)
Variables
Variables are a vital part of any kind of programming. They are used to store text and numbers so they can be easily referenced. Defining a variable is quite simple.
Code:
<?php
$myName = "Danny";
$myAge = "18";
echo "My name is " . $myName . " and i am " . $myAge . " years old.";
?>
This piece of code uses 2 variables, one for name and one for age. In php the dollar($) symbol is used to reference variables followed by the name you give it. The variables can they be "echoed" the same way you "echoed" Hello World. This time you may notice that when you are combining a string and a variable, you have to join the string together with a dot(.). All text should be in quotes(") and all variables need to be defined outside of these quotes as they are not strings.
The if statement
If statements are very useful in php and are used very frequently for comparing data and doing different things depending on the result. If statements are sometimes called selection statements.
Say that i wanted to test if a password entered on my site, was the same as the password stored in the database of my site.
Code:
<?php
$passwordForDatabase="Password1";
$passwordEnteredFromUser="haha";
if ($passwordForDatabase == $passwordEnteredFromUser ) {
echo "Congratulations, you entered the correct password.";
}
else {
echo "Sorry, the password you entered was incorrect";
}
?>
The 2 variable declarations you should spot are pretty basic. It is defining the 2 passwords to be compared. On the IF statement line you will see if is trying to compare the 2 variables to see if they are the same. The '==' means equal too, this is one of many logical operators that can be used.
== means 'equal too'
!= means 'not equal too'
> means 'greater than'
< means 'less than'
>= means 'greater than or equal too'
<= means 'less that or equal too'
$$ means AND
|| means OR
Back to the code. As the variables are not equal too each other than the ELSE statement will be parsed and executed, meaning that the user will receive the 'Sorry, the password you entered was incorrect' message. If you don't believe me, try it :)
Another example of this IF statement can be shown in this example.
Code:
<?php
// this line will not be read by php, this is designed for a single line comment.
/* This is
a multi line
comment */
$number = 4;
$second = 82;
$result = 0;
// I will now add these 2 numbers, if the first number is smaller than the second number.
if ( $number < $second ) {
$result = $number + $second;
}
?>
This concludes the tutorial, and you should now understand some of the basic concepts for PHP. Hopefully :)