If, else, switch commands in PHP
If, elseif ... else and switch commands are used to control the flow based on different conditions.
If, elseif . else and switch commands are used to control the flow based on different conditions.
You can use conditional commands in your code to control the flow. PHP supports the following three control commands:
If . else command - Use this command if you want to execute a code set when a condition is true and another code set if the condition is not true.
The elseif command - Used with the if . else command to execute a code set if one of the conditions is true.
Switch - Used if you want to select one of the code blocks to be executed. The switch command is used to avoid using an if . elseif . else block.
If . Else command in PHP
If you want to execute a code if a condition is true and another code block if a condition is false, you use the if . else command in PHP.
Syntax
if (thing_kids)
This code is executed if the condition is true
else
This code is executed if the condition is false
For example
The following example will give the result "Happy weekend!" If today is Friday. If not, it will result in "Have a happy day!":
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
The elseif command in PHP
If you want to execute a code block if one of the conditions is true, then you should use the elseif command in PHP.
Syntax
if (thing_kien_1)
This code is executed if condition 1 is true;
elseif (thing_kiện_2)
This code is executed if condition 2 is true;
else
This code is executed if the conditions are false;
For example
The following example will give the result "Happy weekend!" If today is Friday, and "Happy Sunday!" If today is Sunday. If not, it will result in "Have a happy day!":
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Switch command in PHP
If you want to select one of the many code blocks to be executed, you should use the switch command in PHP. The switch command is used to avoid if . elseif . else blocks.
Syntax
switch (expression)
{
case label_1:
This code is executed if the expression = label_1
break;
case label_2:
This code is executed if the expression = label_2
break;
.
default:
This code is executed if
expression_ is different from label_1, label_2, .
}
For example
The working mechanism of the switch command is excellent. First, it evaluates the given expression, then finds a label to match the estimated result value. If a match is found, the code associated with the label will be executed or if there is no matching label, the command will execute any given default code block.
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Follow tutorialspoint
Previous article: Operator in PHP
Next article: Loop in PHP
You should read it
- Del command in Windows
- Set command in Windows
- Cmd command in Windows
- Reg command copy in Windows
- Fc command in Windows
- The echo command in Windows
- Command at in Windows
- The nfsshare command in windows
- Doskey command in Windows
- The command reg add in Windows
- Rem command in Windows
- Bitsadmin getdisplayname and bitsadmin geterror command in Windows
Maybe you are interested
How to use Photoshop CS5 - Part 15: Remove wrinkles with the Healing Brush tool The list of foods that contain the most iron is easy to find Full body massage chair of any kind? The benefits can not be ignored when using infrared massage cushion There was a year when the Earth had no summer and the world history was changed 10 types of food enhance men's vitality