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, switch commands in PHP Picture 1If, else, switch commands in PHP Picture 1

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:

If, else, switch commands in PHP Picture 2If, else, switch commands in PHP Picture 2

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:

If, else, switch commands in PHP Picture 3If, else, switch commands in PHP Picture 3

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:

If, else, switch commands in PHP Picture 4If, else, switch commands in PHP Picture 4

Follow tutorialspoint

Previous article: Operator in PHP

Next article: Loop in PHP

5 ★ | 1 Vote