If, else, switch commands in PHP
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
May be interested
- How does the LAN switch (LAN switch) work?to reduce congestion and improve lan speed, many it technicians seek to use lan switching technology and lan switches to improve the performance of traditional ethernet technologies.
- 99+ entertaining AI commandsyou can ask ai to create more for games you already know like monopoly, chinese chess, chess... or set up games, travel and more.
- Extremely dangerous commands on Windows, Linux, Mac, don't try even oncethere are commands that are used to provide certain features to the operating system. however, if used improperly, they cause danger and damage the operating system.
- Guide to network operation for Linux users: 11 commands to knowlinux supports commands to download files, diagnose network problems, manage network interfaces or view network statistics on the terminal. here are some common linux commands to work with, please consult.
- The Mysterious C Button on Nintendo Switch 2the appearance of the c button on the nintendo switch 2 has caused a stir in the gaming community, trying to speculate on its true meaning.
- 15 Tar commands should try in Linuxon * nix operating systems, such as linux, the commonly used utility is tar. the name of this command comes from archive tape, because it was originally designed to back up data on tape.
- Ways to Power Off Nintendo Switchdespite the growth of the smartphone game market, the nintendo switch is still a handheld gaming device with a distinct and appreciated identity.
- How to Run Linux Commands on Windows with WSL 2windows subsystem for linux 2 builds on the success of the original wsl, and the newer wsl 2 brings more power and reliability to users. it is important that you understand how to use linux commands and utilities on windows subsystem for linux 2.
- How to add a passcode to Nintendo Switchjust because you share a console with someone doesn't mean they can see everything in your account. here's how to set up a password (or pin) on your nintendo switch or switch lite.
- Combine all the most basic AutoCAD commandsusing autocad shortcut commands helps to manipulate faster. if you do not know all the cad commands, please see the summary of autocad commands that we have done below.