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

Source: Internet
Author: User
1. If
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:

if (expr)
Statement

As defined in the chapter of an expression, expr evaluates in Boolean. If the value of expr is true,php the 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 "Converting to Boolean" section.

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

<?PHPIF ($a > $b)   print "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) {   print "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.

2. Else
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) {   print "A is bigger than B";} else {   print "A was not bigger than B";}? >

The Else statement is executed only if and when the value of an expression in the ElseIf (if any) statement is false (see ElseIf).

3. ElseIf
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) {   print "A is bigger than B";} elseif ($a = = $b) {   print "A is equal to B";} else {   PR int "A is smaller than B";}? >

There can be multiple ElseIf statements in the same if structure. The ElseIf statement (if any) with the first expression value of true will be 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 (which is the same behavior if you are familiar with C language), but the bottom line is that both produce exactly the same behavior.

The ElseIf statement is executed only if the expression value of the previous if or ElseIf is false, and the current ElseIf expression evaluates to True.

4. Else
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) {   print "A is bigger than B";} else {   print "A was not bigger than B";}? >

The Else statement is executed only if and when the value of an expression in the ElseIf (if any) statement is false (see ElseIf).

Related Article

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: info-contact@alibabacloud.com 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.