PHP 5 Conditional Statements
Conditional or control Statements
Conditional Statements are those statements that have the ability to make decisions based on the given data, like if condition is true then perform addition otherwise perform subtraction. PHP provides some conditional statement to achieve this goal.
There are mainly two types of conditional statement
- If else statement
- Switch statement
If else statement:-
"If else" statement is used, if a block of code must test a specific condition,"if condition tested and return true then if block executed otherwise (if condition tested and return false)" else" block executed.
Switch statement is used when no of condition are more than one. It tested condition and execute a particular block of code from multiple blocks of codes.
Advantage of switch over if-else
Switch statement reduces the complexity of if else statement.
You can make multiple decision by using else-if but it's more complex than switch statement.
Looping statement are also conditional statement. The loops in PHP are used to execute a block of code again and again as per the condition.
There are four loop available in PHP.
- For loop
- While loop
- Do while loop
- Foreach loop
Advantage of looping
- The main advantage of using looping is optimization of code.
- Reduce the number of line in the code.
- Reduce the complexity of the code.
All loops and conditional statements are explained in the next sections.