In Part 1 of our PHP Tutorial series we covered how to use PHP and HTML together and use the print command.
In this one we will learn to use variables and use the date command. If you have never programmed before then the concept of a variable may be new to you. A variable is like a box in which you can store something for later. For example you may store the date or name of a person in a variable so that you can use it later in the code. A quick example is to open notepad again and add the following code then save it as a .php file and upload to your site:
[code='php']
$variable1="Bob";
$last_name="Smith";
Print("Hello my name is $variable1 $last_name");
?>[/code]
In the above example there are 2 variables ($variable1 and $last_name). When using a variable the name needs to start with a dollar sign ($) and not have any spaces in it. If you are setting the variable to be text then you will need to use an equals with the text in brackets. Remember that all lines in PHP end with a semicolon (;).
A variable does not have to be text but can be a function. For example if we wanted to display the date we could do the following:
[code='php']
$now=date("F j, Y, g:i a");
Print("The date today is $now");
?>[/code]
Again be sure to save the file with a .php extension and upload it to your site. In this example you should see the current date.
Stay tuned as we will continue this tutorial with more details on using PHP.