
Effective Usage of Conditional Statements in PHP for Code Decision Making
Learn how to effectively utilize conditional statements such as if, if...else, and switch in PHP to make informed decisions within your code. From executing code based on a single condition to handling multiple conditions using elseif, this guide provides practical examples and insights into writing structured code for better functionality.
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Very often when you write code, you want to perform decisions. statements in your code to do this. In PHP we have the following conditional statements: if statement if if...else statement if if... switch statement different You actions can for different conditional use if statement ...else statement ...elseif switch statement elseif....else statement ....else statement
The if statement is used to execute some code only Syntax if (condition) { code to be executed if condition is true; } only if if a a specified Syntax specified condition condition is is true true.
The example below will output "Have a good day!" if the current time (HOUR) is less than 20: <?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } ?>
Output : Have a good day!
Use the if....else statement to execute some code if if a a condition the Syntax if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } condition is is true condition is is false Syntax true and and another another code code if if the condition false.
The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise: <?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
Use the if....elseif...else statement to specify new false 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; } specify a a condition is is new condition false. . Syntax condition to to test, test, if if the the first first condition
The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!": <?php $t = date("H"); if ($t < "10") { echo "Have a good morning!"; } elseif ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
Output : The hour (of the server) is 03, and will give the following message: Have a good morning!
The switch statement is used to perform different conditions. Use the switch statement to select many actions based on different select one executed. one of of many blocks blocks of of code code to to be be executed
Syntax switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; } Syntax
How switch statement works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break the statement is used if no match is found. break to prevent the code from running into next case automatically. The default default
<?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?>
Output: Your favorite color is red!