Summarize the use of if, else, elseif, else conditional judgment statements

伊谢尔伦
Release: 2023-03-11 06:40:02
Original
8594 people have browsed it

1. if
The if structure is one of the most important features of multilingualincluding php, which allows code fragments to be executed conditionally. The if structure of PHP is similar to the C language:

if (expr)
statement

As defined in the chapter Expression , expr is evaluated as a Boolean. If the value of expr is true, php will execute the statement, if the value is false - the statement will be ignored. See the "Converting to Boolean Values" section for more information on which values ​​are considered false.

If $a is greater than $b, the following example will show that a is bigger than b:

<?php
if ($a > $b)
   print "a is bigger than b";
?>
Copy after login

It is often necessary to execute more than one statement according to conditions. Of course, there is no need to add Previous if clause. These statements can be placed into statement groups. For example, if $a is greater than $b, the following code will display a is bigger than b and assign the value of $a to $b:

<?php
if ($a > $b) {
   print "a is bigger than b";
   $b = $a;
}
?>
Copy after login

if statements can be nested infinitely deep inside other If statement, this provides sufficient flexibility for conditional execution of different parts of the program.

2. else
It is often necessary to execute a statement when a certain condition is met, and execute other statements when the condition is not met. This is the function of else. else extends the if statement to execute the statement when the expression in the if statement evaluates to false. For example, the following code displays a is bigger than b when $a is greater than $b, and otherwise displays a is not bigger than b:

<?php
if ($a > $b) {
   print "a is bigger than b";
} else {
   print "a is not bigger than b";
}
?>
Copy after login
Copy after login

else statement is only used in if and elseif (if any) statements Executed when the expression evaluates to false (see elseif).

3. elseif
elseif, as the name implies, is a combination of if and else. Like else, it extends the if statement and can execute a different statement when the original if expression evaluates to false. But unlike else, it only executes the statement when the conditional expression of elseif evaluates to true. For example, the following code will display a is bigger than b, a equal to b or a is smaller than b respectively according to the conditions:

<?php
if ($a > $b) {
   print "a is bigger than b";
} elseif ($a == $b) {
   print "a is equal to b";
} else {
   print "a is smaller than b";
}
?>
Copy after login

There can be multiple elseif statements in the same if structure. The first elseif statement (if any) whose expression evaluates to true will be executed. In php, it can also be written as "else if" (two words), which is exactly the same behavior as "elseif" (one word). There is a slight difference in the meaning of syntax analysis (it's the same behavior if you're familiar with C), but the bottom line is that both produce exactly the same behavior.

elseif statement is only executed when the previous if or elseif expression value is false and the current elseif expression value is true.

4. else
It is often necessary to execute a statement when a certain condition is met, and execute other statements when the condition is not met. This is the function of else. else extends the if statement to execute the statement when the expression in the if statement evaluates to false. For example, the following code displays a is bigger than b when $a is greater than $b, and otherwise displays a is not bigger than b:

<?php
if ($a > $b) {
   print "a is bigger than b";
} else {
   print "a is not bigger than b";
}
?>
Copy after login
Copy after login

else statement is only used in if and elseif (if any) statements Executed when the expression evaluates to false (see elseif).

The above is the detailed content of Summarize the use of if, else, elseif, else conditional judgment statements. 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
Popular Tutorials
More>
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!