If ... Else statement
Use the If....else statement if you want to execute some code when a condition is set up and execute some other code when the condition is not valid.
Grammar
The code is as follows |
Copy Code |
if (condition)//Set condition Code to was executed if condition is true; If the condition is true, the code is executed; Else Code to was executed if condition is false; If the condition is false, the code is executed |
Instance 1
If the current date is Monday, the following code will output "Happy Monday." "Or else it will output" happy every day. ”:
The code is as follows |
Copy Code |
<?php $d =date ("D"); Variable d Assignment if ($d = = "Mon") { echo "Monday happy!" "; }else{ echo "Happy every day!" "; } ?> |
Instance 2
If D equals 1 o'clock Output "number 1", otherwise the output "number is not 1"
The code is as follows |
Copy Code |
<?php $d = 2; Variable d Assignment if ($d ==1) { echo "Number 1"; Value of variable D equals 1 o'clock output }else{ echo "Number is not 1"; Values that are not equal to 1 o'clock output } ?> |
ElseIf statement
If you want to execute code when one of several conditions is true, use the ElseIf statement:
Grammar
The code is as follows |
Copy Code |
if (condition)//Condition 1 Code to was executed if condition is true; Condition 1 is true, code is executed ElseIf (condition)//Condition 2 Code to was executed if condition is true; Condition 2 is true, code is executed Else Code to was executed if condition is false; When all 2 conditions are false, the code is executed |
Instance
When the value of D equals 1 o'clock, the output "number 1" is equal to 2 o'clock output "number 2" and output "no match number" when none of the two conditions are true.
The code is as follows |
Copy Code |
<?php $d = 3; Variable d Assignment if ($d ==1) { echo "Number 1"; When d equals 1 o'clock Output "Number 1" } ElseIf ($d ==2) { echo "Number 2"; When d equals 2 o'clock Output "Number 2" } else{ echo "does not conform to the number"; Two conditions are not established when the output of the content } ?> |
Summary in if else and ElseIf if can include have else and elseif but if you use the if else will not appear elseif but instead, we can see from the above example, more PHP tutorials can be clicked on the connection view.