Decision Making Statements
Decision making statements in PHP are required to specify one or more conditions that will be evaluated or tested and then a certain set of statements are executed accordingly. The flowchart for decision making statements can be presented as:
Conditional Statements in PHP are:
If else statement:
This statement will execute a set of instructions if the condition is TRUE and execute another set of instructions if the condition is FALSE.
//Syntax
if(condition){
statements to execute if condition is TRUE
}else{
statements to execute if condition is FALSE
}
elseif statement
elseif statement is used to deal with multiple set of conditions.
// Syantax:
if(condition) {
statements to execute if condition is TRUE
}elseif(condition) {
statements to execute if first condition is FALSE and this condition is TRUE
}else{
statements to execute if all above conditions are FALSE
}
switch statement
Switch statement is similar to multiple elseif blocks. It checks the value of a variable, compares it with multiple cases and an associated particular case is executed.
// Syntax:
switch (variable) {
case 1:
statements to execute if variable = 1;
break;
case 2:
statements to execute if variable = 2;
break;
case 3:
statements to execute if variable = 3;
break;
....
default:
statements to execute if variable is different from all case values;
}