Inphpif can use direct ifelseor ElseIf to determineotherpossible situations. Let’s introduce if else and ElseIf There are differences in usage and some details.
If…Else Statement
If you want to execute some code when a certain condition is true and other code when the condition is not true, use the if….else statement.
Syntax
if (condition) //设置条件 code to be executed if condition is true; //如果条件为真的代码被执行; else code to be executed if condition is false; //如果条件为假,代码被执行
Example 1
If the current date is Monday, the following code will output "Happy Monday.", otherwise it will output "Happy Every Day.":
Example 2
If d is equal to 1, output "number 1", otherwise output "number is not 1"
ElseIf statement
If you want to be in multiple To execute the code when one of the conditions is true, please use the elseif statement:
Syntax
if (condition) //条件1 code to be executed if condition is true; //条件1为真,代码被执行 elseif (condition) //条件2 code to be executed if condition is true; //条件2为真,代码被执行 else code to be executed if condition is false; //2个条件都为假时,代码被执行
Example
When the value of d is equal to 1, output "number 1"; equal to 2 When the two conditions are not met, it outputs "No matching number"
Summary: in if else and ElseIf, if can include else and elseif, but if after using if else Otherwiseif will not appear but the opposite will work.
The above is the detailed content of An example of the difference between if else and else if in php. For more information, please follow other related articles on the PHP Chinese website!