An example of the difference between if else and else if in php

伊谢尔伦
Release: 2023-03-11 07:58:01
Original
3156 people have browsed it

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; //如果条件为假,代码被执行
Copy after login

Example 1
If the current date is Monday, the following code will output "Happy Monday.", otherwise it will output "Happy Every Day.":

Copy after login

Example 2

If d is equal to 1, output "number 1", otherwise output "number is not 1"

Copy after login

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个条件都为假时,代码被执行
Copy after login

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"

Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!