Home > Backend Development > PHP Tutorial > php--if/else/elseif/else if

php--if/else/elseif/else if

伊谢尔伦
Release: 2016-11-24 09:29:44
Original
1504 people have browsed it

if

(PHP 4, PHP 5)

if structure is one of the most important features of many languages, including PHP, which allows code fragments to be executed conditionally. PHP's if construct is similar to the C language:

<?php
if (expr)
  statement
?>
Copy after login

As defined in the Expressions chapter, expr evaluates to 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 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)
  echo "a is bigger than b";
?>
Copy after login

It is often necessary to execute more than one statement conditionally, and of course there is no need to add an if clause to each statement. 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) {
  echo "a is bigger than b";
  $b = $a;
}
?>
Copy after login

if statements can be nested infinitely deep inside other if statements, which gives the program Conditional execution of different parts provides full flexibility.

else

(PHP 4, PHP 5)

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 exactly the function of else. else extends the if statement and can 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, otherwise it displays a is NOT bigger than b:

<?php
if ($a > $b) {
  echo "a is greater than b";
} else {
  echo "a is NOT greater than b";
}
?>
Copy after login

else statement only contains the value of the expression in the if and elseif (if any) statement Executed when FALSE .

elseif/else if

(PHP 4, PHP 5)

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

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

There can be multiple elseif parts in the same if statement, where the first expression value is TRUE The elseif part (if any) will be executed. In PHP, you can also write "else if" (two words), which behaves exactly like "elseif" (one word). There is a slight difference in the meaning of syntax analysis (and the same behavior if you are familiar with C), but the bottom line is that both produce exactly the same behavior.

elseif statements are only executed when the previous if and all previous elseif expressions evaluate to FALSE, and the current elseif expression evaluates to TRUE.

Note: It must be noted that elseif and elseif are considered to be exactly the same only when curly braces are used in the above example. If you use a colon to define an if/elseif condition, you cannot use a two-word else if, otherwise PHP will generate a parsing error.


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