In the previous chapters the basics of PHP have been explained. With this you must be well versed with the basic idea of PHP. Now we will be taking a step forward and moving on to the learning of the conditional statements and looping structures. These conditional statements and looping structures give you more flexibility in writing a PHP script.
Conditions and conditional statements are a part of every programming language and scripting language. There are times when you want to perform different actions for different decisions. In this you have to use conditional statements, so that the correct decision should be made.
Consider a situation if you want to greet every visitor on your site on 25 th December with Merry Christmas. In order to achieve this you do not have write a code on Christmas displaying Merry Christmas. This can be done well in advance as you can always write a conditional statement that if date is 25 th December then Merry Christmas should be displayed. So, with the help of conditional statements job done, well in advance.
In PHP there are two conditional statements:
The “if else” statement is used if one set of code has to be executed when the condition is true and another set of code has to be executed when the condition is false.
| Syntax |
|
|
In the following example x is compared with y. If x is greater than y then it will display x is greater then y else it will display y is greater than x.
|
Example |
<html> |
|
|
Output

In the if statement if you want to execute more than one line when the condition is true then those lines should be put in curly braces.
|
Example |
|
|
|
|
Output

The “switch” statement is another useful conditional statement. It is used when one of the many blocks of code has to be executed. When the switch statement is used then it checks for all the conditions at once and it is also a good programming practice.
In the switch statements break is used to prevent the code from running into the next case automatically.
| Syntax |
switch (expression) |
In the following example there is a single expression that is evaluated once. After this the value of the expression is compared with the value of each case in the structure. If the value in any of the cases gives a match, then the block of code associated with that structure is associated. If the value does not match with any of the cases then the default case is executed.
|
Example: |
|
|
Output

In the previous section you learned about the conditional statements. In this section you will be learning about the looping structures. The looping statements are used to execute a same block of code a specified number of times.
The various looping statements available in PHP are:
While – it loops through a block of code if a specific condition is true and it comes out of the loop only when that condition becomes false.
Do while – in this it loops through a block of code at least once, then it checks for the condition and loops as long as that condition is true.
For – it loops through a block of code a specified number of times.
Foreach – it loops through a block of code for each element in an array.
The “while” loop, is the simplest type of loop in PHP. They work the same in PHP as they do in other languages. The “while” statement executes a block of code if a specific condition is true and it comes out of the loop only when that condition becomes false.
| Syntax |
|
while (condition) code to be executed; |
In the following example we are using the while loop which checks for the condition and executes the loop till the condition is true.
|
Example |
|
|
Output:

The “do while” statement executes the loop at least once. Then it checks for the condition and repeats the loop as long as the condition is true.
The basic difference between the while and do while loop is that in while loop the condition is first checked and then loop is executed. In do while loop the loop is executed at least once and then the condition is checked. If the condition is true only then the loop will execute further.
| Syntax |
do |
In the following example we are using the do while loop which executes the block of code at least once and then checks the condition. If the condition is true it will execute the block of code till the condition becomes false.
|
Example: |
|
|
Output

The “for” loop in PHP works in the same way as it does in other languages. The “for” loop is used when you already know that how many times a statement or a block of statements has to be executed.
The “for” loop has three parameters. The first parameter in the for loop initializes the variable, the second parameter checks for the condition and the third parameter is for increments required to implement the loop. If there is more than one variable in either the initialization or increment part then they should be separated by commas.
| Syntax |
|
For (initialization; condition; increment)
|
In the following example “Welcome to expertrating.com!” has to be displayed for five times on the browser. So, for this we use the “for” loop as we already know that for how many times the loop should run.
|
Example |
|
|
Output

The foreach loop works only on arrays. It loops through a block of code for each element in an array. On each loop the value of the current element in array is assigned to $value and the array pointer is advanced by one. Similarly in the next loop the value of the next element in array is assigned to $value and the array pointer is advanced by one. This process is repeated till the end of the array.
| Syntax |
foreach (array as value) |
In the following example the values of the given array will be printed by the loop.
|
Example |
<html> |
Output
