loading...

Decision making in PHP.(module 3)

sannilincoln profile image sannilincoln ・2 min read

Like most programming languages, PHP is built with conditional statements for making decision based on different conditions.
The if statements execute code if one condition is true. the syntax is
if(condition){
code to be executed;
}
The if statement is suitable when there is only one condition to be tested.

The if else if statements execute code if a condition is true and if false another code is executed. the syntax is
if(condition){
code to be executed
} else{
code to be executed;
}
It is suitable when there are only two conditions to be tested.
The nested if is used when the condition to be tested is more than two. the syntax is
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if the first condition is false and this condition is true;
} else{
code to be executed if all conditions are false;
}
The switch case conditional statement is used to perform different actions based on different conditions. The switch statement selects one of many blocks of code to be executed. the syntax is
switch (a) {
case label1:
code to be executed if a=label1;
break;
case label2:
code to be executed if a=label2;
break;
case label3:
code to be executed if a=label3;
break;
default:
code to be executed if a is different from all labels;
}
The first single expression (a) which is often a variable is evaluated once. then the value of the expression is then compared with the values for each case in the structure. If the value of the expression matches any one of the cases, the block of code associated with the code is executed. the break statement is used to prevent the code from running into the next case automatically. the code in the default statement is executed if there no match found.
The jump statements are keywords which are used in moderating the conditional statements wether to break, continue or exit the running of code. The break statement is usually used in loop or switch statement. The code will stop working if the condition is true.
The continue statement skip the program execution and go to the next program. and the exit statement terminates the program's current execution flow.

Posted on by:

Discussion

markdown guide