The beginners guide to PHP
The first thing that you are going to need to know about PHP is that it is built around HTML. Any PHP page can function the same as an HTML page. So if we were to go and put a head, body and some content in paragraphs you would have a successful HTML page being outputted by PHP.
1 2 3 4 5 6 7 8 | <head> <title>My PHP Page</title> </head> <body> <h1>The Main Test Page</h1> <p>This is the paragraph</p> </body> |
When you run the following lines inside of a PHP script you are essentially just coding in HTML but as a .PHP file.
Anything that you code in PHP must be between the PHP tags. If it is not between the PHP tags the browser will assume that it is HTML and will print out simple text from the browser.
1 2 3 4 5 | <?php ?> <? ?> |
As you can see there are two different ways to show that the code is PHP. It is more formal among coders to use the first method of opening and closing tags but it is not necessary. It is more or less a way to recognize that the following code is PHP based.
It is always a good idea to properly comment the code that you are working on. This will help people that read your code if you ever need help with it. Also when going back to a project after quite some time you may be forgetful as tow hat variables and sections of the code are doing what. Which leads me to want to talk about assigning variables.
Unlike other programming languages where declaring a variable as an Integer, Boolean, Float, Etc is important and needed to get the program to run. In PHP you can convert variables to these values but if you wanted to assign a variable with a number or text it only requires an equal sign.
1 2 3 | <?php //this is a comment ?> |
Declaring a variable.
1 2 3 4 5 6 7 | <?php // declaring a variable $pizza = “$13.99″; // printing the variable echo $pizza; ?> |
Notice how the variable has a $ sign on it. All variables must have this dollar sign in front of it in order to be noticed by the server processing the PHP. A variable can contain almost any information that you want and can be called anything, but it is a good idea to name them something that will help you remember them. If you have variables of $q, $h, $ht, $gh and so on. Chances are that you will be ridiculed by someone eventually. You are also going to have issues remembering the code that you have entered.
Functions are a very important part of PHP much like they are for any language. In this next example we are going to create our own function and apply it to some code with a variable and content outputs into a browser.
1 2 3 4 5 6 | <?php function roll() { return mt_rand(1,6); } echo roll() ?> |
As you can see, we have created a rolling dice with PHP. Within this function we included the “mt_rand”, similar to rand but has a more random output then the simple “rand.”
Now we are going to load a variable from the roll function that we have created then output it into the browser.
1 2 3 4 5 6 7 8 | <?php function roll() { return mt_rand(1,6); } $roll1 = roll() echo “you have rolled a ” . $roll1 . “, way to go!” ?> |
With this short section of code you first create the function roll. After the function you then load it into the variable roll1. Now you have an output of this roll going to your browser. This output will say for example “ you have rolled a 1, Way to go!”

