08 Jul PHP Decision Making Statements
We saw in the previous lesson, how to work with PHP loops, while, do..while, for, and for…each. Now, we learn PHP statements with examples and illustrations. With PHP decision-making statements, you can easily take decisions based on different conditions.
Let’s discuss the decision-making statements,
- if statement
- if…else statement
- if…else if…else statement
- switch statement
if statement
The if decision-making statement executes the code, if the condition is true.
Here’s the syntax,
1 2 3 4 5 |
if (condition) { code will execute if condition is true; } |
Example
Here, the code executes if the condition $a > 5 is true. The value of a is greater than 5, so the if statement executes and prints, We’re learning PHP,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <body> <?php $a = 10; if ($a > 5) { echo "We’re learning PHP!"; } ?> </body> </html> |
Here’s the output,
if…else statement
The if…else decision-making statement executes some code if the condition is true and another code if the condition is false. The false condition comes under else.
Here’s the syntax,
1 2 3 4 5 6 7 |
if (condition) { code will execute if condition is true; } else { code will execute if condition is false; } |
Example
The value of $a = 10, so the first statement won’t execute, since its condition is $a < 5.
However, the second statement else executes and prints We’re learning Python as shown below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html> <body> <?php $a = 10; if ($a < 5) { echo "We’re learning PHP!"; } else { echo "We’re learning Python!"; } ?> </body> </html> |
Here’s the output,
if…else if…else Statement
The if…else if…else statement executes code for different conditions. If more than 2 conditions are true, you can use else for the false condition.
Here’s the syntax,
1 2 3 4 5 6 7 8 9 |
if (condition) { code will execute if condition is true; } else if (condition) { code will execute if another condition is true; } else { code will execute if all the above given conditions are false; } |
Example
Here, $a = 5, so the else condition gets executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html> <body> <?php $a = 5; if ($a < 5) { echo "We’re learning PHP!"; } else if ($a > 5) { echo "We’re learning Python!"; } else { echo "We’re learning Java!"; } ?> </body> </html> |
Here’s the output,
PHP Switch Statement
If you want to avoid using long code for if…elseif…else, then use the switch statement. Under switch, you can add many blocks of code and select one at a time.
Here’s the syntax,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// c is the expression switch (c) { case label1: code to be executed if c=label1; break; case label2: code to be executed if c=label2; break; case label3: code to be executed if c=label3; break; case label4: code to be executed if c=label4; break; ... default: this get executed if c is different from all labels; } |
Example
Here, the Java topic gets printed as output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!DOCTYPE html> <html> <body> <?php $topics = "Java"; switch ($topics) { case "Android": echo "Topic: Android"; break; case "Java": echo "Topic: Java"; break; case "Python": echo "Topic: Python"; break; default: echo "Topic: Management"; } ?> </body> </html> |
Here’s the output,
Here, we saw how to work with PHP Decision-making statements, if, if..else, if…else…elseif, switch, etc.
No Comments