PHP Basics – a crash course

This post is my attempt to give something back. My aim is to help anyone who might want to begin web development using PHP. I have written the tutorial below. I hope you find it useful. If anyone has any feedback, both positive and negative, please either post a comment below or email me directly. Your comments will help me to improve this tutorial.

Many other programming tutorials start with a script that simply prints the text ‘Hello World’ to the screen. This tutorial will begin a step above that in order to make it a little more interesting and provide you with a sense of accomplishment at the end.


As you might be aware, PHP stands for Hypertext Preprocessor. It is a server-side scripting language and is a powerful technology for developing web applications and database driven websites.

Problem Statement:

Fizz Buzz game: write a script in PHP that counts from 1 to 100. Where 5 or a multiple of 5 occurs, print the word ‘Fizz’. Where 7 or a multiple of 7 occurs, print the word ‘Buzz’. Where a number is a multiple of 5 and 7, print the words ‘Fizz Buzz’. Any where that is not a 5, 7, or multiple of 5 and 7, print the number.


Step 1:

You will need some way of running / testing your PHP script. You can either use one of the many hosting providers’ servers, install PHP onto your computer, or install XAMPP onto your computer.

Step 2:

Open a text editor such as Notepad, Notepad++, PS Pad, etc. I use gedit (available on Ubuntu). Start by typing the open and close brackets that tell the server that you are using PHP code and the PHP interpreter is required.


Step 3:
Declare variable which is an element used for storing some piece of information. In this case, the variable will store the number and keep track the count. We will declare ‘num’ as the variable and store the number ‘1’. This will be where we will start counting. Note that each statement in PHP requires a semi-colon at the end. This tells the interpreter that we have finished that particular statement.

$num = 1;

Step 4:
We will use a for loop to loop or iterate through each number until we reach 100. What the code below does is start at 1 and run for 100 iterations.
for ($i = 1; $i <= 100; $i++){

Step 5:
We are now going to use if and else if statements to test for a possilble ‘Fizz’, ‘Buzz’, or ‘Fizz Buzz’. The operators used are % which divides a number by another number and gives us the remainder, the == which tests if a number is equal to another number, && which means that both conditions must be met, || means one or the other of the conditions must be met. The condition is the portion of the if or else if that is contained in the rounded brackets / braces.

if (($num % 5 == 0) && ($num % 7 == 0)){
echo 'Fizz Buzz<br/>';

} else if (($num == 5) || ($num % 5 == 0)){
echo ‘Fizz<br/>’;

} else if (($num == 7) || ($num % 7 == 0)){
echo ‘Buzz<br/>’;

} else{
echo $num.'<br/>’;


The tests we are doing are: first look for multiples of both 5 and 7, then look for just 5 or multiples of 5, and then look for 7 or multiples of 7. If no condition is met, use the else portion of the if statement to print the number.

Now, that wasn’t so bad was it ? Do give this a try yourself and if you have any questions, please do not hesitate to get in touch. I am quite happy to receive questions via email.

If you found this tutorial helpful, feel free to tell your friends about it or you can send me a small donation as a thank you or kind of gratuity / tip.

Click here to view this script in action

To view the complete code, click below.

Spoiler Inside SelectShow


Leave a Reply

This entry was posted on Sunday, April 7th, 2013 at 6:11 pm and is filed under PHP, Web. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.