PHP process control elseif/else if

不言
Release: 2023-03-25 10:48:02
Original
1644 people have browsed it

This article mainly introduces elseif/else if about PHP process control. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.

This article is used for basic purposes. Learners, experts please close this page

Read this article for 3 minutes, it’s hard to understand it

(PHP 4 , PHP 5, PHP 7)

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 value is 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

in the same There can be multiple elseif parts in an if statement, and the first elseif part whose expression value is TRUE (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. The

elseif statement is only executed when the previous if and all previous elseif expression values ​​are FALSE, and the current elseif expression value is 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.

<?php

/* 不正确的使用方法: */
if($a > $b):
    echo $a." is greater than ".$b;
else if($a == $b): // 将无法编译
    echo "The above line causes a parse error.";
endif;


/* 正确的使用方法: */
if($a > $b):
    echo $a." is greater than ".$b;
elseif($a == $b): // 注意使用了一个单词的 elseif
    echo $a." equals ".$b;
else:
    echo $a." is neither greater than or equal to ".$b;
endif;

?>
Copy after login

Related recommendations:

php flow control else

php flow control if statement

The above is the detailed content of PHP process control elseif/else if. 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