In this tutorial you will learn about the PHP Decision Making and its application with practical example.
Decision Making Statement allows computer to decide block of code to be execute based on the condition.if, if … else,if … else if and switch statements are used as decision making statements in PHP.
The if Statement in PHP
Block of statement executed only when a specified condition is true.
1 |
if (condition){ //Block of code to be executed comes here if condition is true;} |
The If…Else in PHP
When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.
1 2 3 4 |
if (condition) //Block of code to be executed comes here if condition is true; else //Block of code to be executed comes here if condition is false; |
The if … else if Statement in PHP
The if….elseif…else statement is used to select one of among several blocks of code to be executed.
1 2 3 4 5 6 7 8 9 10 11 12 |
if (condition) { //Block of code to be executed comes here if condition is true; } elseif (condition) { //Block of code to be executed comes here if condition is true; } else { //Block of code to be executed comes here if condition is false; } |
The Switch case statement in PHP
The switch statement is simplified form of the nested if … else if statement , it avoids long blocks of if..elseif..else code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch (expression) { case val1: //Block of code to be executed if expression = val1; break; case val2: //Block of code to be executed if expression = val2; break; default: //Block of code to be executed //if expression is different //from both val1 and val2; } |