Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!

# PHP Loop

## Conditional Statements

If statement:
• The if statement is used when you want an action to be performed only when a specified condition is true.

• SYNTAX:

If else statement:
• The if else statement is used when you have a course of action to be taken if the condition is true and an another course of action to be taken if the condition is true.

• SYNTAX:

NOTE: You can also have nested if else statements like ifelseifelse where only one correct block will be executed.

Switch statement:
• The switch statement is used when one out of many blocks are to be executed.

• SYNTAX:

## LOOPS

WHILE LOOP
• A while loop executes a block of statement till a particular condition is true.

• SYNTAX:
• If the condition is false then the while loop is not executed.

• DO WHILE LOOP
• A do while loop also executes a block of statements till a particular condition is true.

• SYNTAX:

NOTE: The main difference between a while loop and a do while loop is that a do while loop executes first and then checks the condition whereas while first checks the condition and then executes the block.

FOR LOOP
A for loop is used when you know in advance the number of times you want the block to be executed. SYNTAX: for(initialize counter; test counter; increment counter) { .... }

FOREACH LOOP
• A foreach loop is used in an array. It is used to visit each index of the array.

• Break and Continue
• Break in used to exit the innermost loop in which it is declared whereas continue is used to stop the current iteration of the program and got to the next iteration.

• E.g.:
• Here, when continue is encountered it terminates the current iteration checks the value of \$x and then increments it and thus the iteration continues till the loop is not terminated.

• ## EXAMPLES

(1)

(2)

(3)

(4)

NOTE: ++\$X stands for pre increment & \$x++ stands for post increment. Similarly for decrement. For information about operators visit http://www.w3schools.com/php/php_operators.asp)

CACKLE comment system