Php--if/else/elseif/else if

Source: Internet
Author: User


(PHP 4, PHP 5)

The IF structure is one of the most important features in many languages, including PHP, which allows code snippets to be executed on a conditional basis. The IF structure of PHP is similar to the C language:

<?PHPIF (expr)  statement?>

As defined in the chapter of an expression, expr evaluates in Boolean. If the value of expr is true,php, statement will be executed, and if the value is false--, statement will be ignored. For more information about which values are treated as FALSE, see the Convert to Boolean section.

If $a is greater than $b, the following example shows a is bigger than B:

<?PHPIF ($a > $b)  echo "A is bigger than B";? >

It is often necessary to execute more than one statement according to the conditions, and it is not necessary to add an IF clause to each statement. You can put these statements in a statement group. For example, if $a is greater than $b, the following code displays a is bigger than B and assigns the $a value to the $b:

<?PHPIF ($a > $b) {  echo "A is bigger than B";  $b = $a;}? >

If statements can be nested indefinitely in other if statements, which provides sufficient flexibility for conditional execution of different parts of the program.


(PHP 4, PHP 5)

It is often necessary to execute a statement when a condition is met, and to execute other statements when the condition is not satisfied, which is the function of else. else extends the IF statement, which executes the statement if the value of the expression in the IF statement is FALSE. For example, the following code shows a is bigger than B when the $a is greater than $b, whereas the opposite shows a is not bigger than B:

<?PHPIF ($a > $b) {  echo "A is greater than B";} else {  echo "A was not greater than B";}? >

The Else statement executes only if the value of an expression in the IF and elseif (if any) statement is FALSE.

Elseif/else if

(PHP 4, PHP 5)

ElseIf, as implied by this name, is the combination of if and else. As with else, it extends the IF statement and can execute a different statement if the original if expression has a value of FALSE. However, 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:

<?PHPIF ($a > $b) {    echo "A is bigger than B";} elseif ($a = = $b) {    echo "A is equal to B";} else {    echo "A is smaller than B";}? >

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

The ElseIf statement is executed only if the previous if and all previously ElseIf expression values are FALSE, and the current ElseIf expression evaluates to TRUE.

Note: It must be noted that ElseIf and else if you use curly braces in a similar case, it is only considered identical. If you use a colon to define the If/elseif condition, you cannot use the else if for two words, or PHP will produce parsing errors.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.