Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we will be discussing about PHP Decision Making.
The if, elseif .....else and switch statements are used to make vital decisions based on different coditions.
The if, elseif .....else and switch statements are used to make vital decisions based on different coditions.
Conditional Statements
Conditional statements can be used in your code to make decisions. PHP supports the following three conditional statements -
Flowchart
- if...else statement - Make use of this statement if you want to execute a set of code when a given condition is true and another if the condition isn't true.
- elseif statement - It is used with the if...else statement to execute a set of code if one of the several conditions is true.
- switch statement - is used if you want to select one of many blocks of code to be executed, use switch statement. The switch statement is used to avoid long blocks of if...elseif...else codes.
READ: PHP Ternary Operator
The If...Else Statement
If you want to execute a code if a condition is true and another code if a condition isn't true (false), then use the if...else statement.
Flowchart
Syntax
if (condition) code to be executed if condition is true; else code to be executed if condition is false;
Example
The following code is going to output "have a nice weekend!" if the current day is friday. Else, it will output "have a nice day!".
<html> <body> <?php $d = date("D"); if ($d == "Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html>
Output
When the above code is executed, it will produce the following result -
Have a nice weekend!
READ: A Guide to PHP Operators
The Elseif Statement
If you intend to execute some PHP codes if one of the several given conditions are true, then use the elseif statement.
Flowchart
Syntax
if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false;
Example
The following code is going to output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if current day is Sunday, otherwise it will output "Have a nice day!".
<html> <body> <?php $d = date("D"); if ($d == "Fri") echo "Have a nice weekend!"; elseif ($d == "Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html>
Output
When the above code is executed, it will produce the following result -
Have a nice Weekend!
Switch Statement
If you want to select one of many blocks of code to be executed, then make use of the switch statement.
The switch statement is used to avoid long blocks of if...elseif...else code.
The switch statement is used to avoid long blocks of if...elseif...else code.
Flowchart
Syntax
switch (expression){ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
Example
The switch statement works in an unusual manner. First it evaluates given expression and then searches for a label to match the resulting value. If it finds a matching value, then the code associated with the matching label will be executed or if none of the label matches any value, then the statement will execute any default code.
<html> <body> <?php $d = date("D"); switch ($d){ case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "Wonder which day is this ?"; } ?> </body> </html>
Output
When the above code is executed, it will produce the following result -
Today is Monday
READ: PHP Basic Syntax
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Loops.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.