Php if elseif condition judgment statement usage

Source: Internet
Author: User
Tags php tutorial

Any php Tutorial script consists of a series of statements. A statement can be a value assignment statement, a function call, a loop, or even a (null statement) condition statement that does nothing. The statement usually ends with a semicolon. In addition, you can use curly brackets to encapsulate a group of statements into a group of statements. A statement group can be considered as a row of statements. This chapter describes various statement types.

If
The if structure is one of the most important features of many languages, including php. It allows code snippets to be executed according to conditions. The if structure of php is similar to that of c:


If (expr)
Statement


 

 

As defined in the expression chapter, expr is evaluated by Boolean. If the value of expr is true, php executes statement. If the value is false, statement is ignored. For more information about which values are considered false, see "convert to Boolean.

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


<? Php
If ($ a> $ B)
Print "a is bigger than B ";
?>

You often need to execute more than one statement according to the conditions. Of course, you do not need to add an if clause to each statement. You can add these statements to the statement group. For example, if $ a is greater than $ B, the following code displays a is bigger than B and assigns the value of $ a to $ B:

 

<? Php
If ($ a> $ B ){
Print "a is bigger than B ";
$ B = $;
}
?>

 

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


Else
It is often necessary to execute a statement when a condition is met, but other statements when the condition is not met. This is exactly the else function. Else extends the if statement and runs the statement when the expression value in the if statement is 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 ){
Print "a is bigger than B ";
} Else {
Print "a is not bigger than B ";
}
?>


The else statement is executed only when the value of the expression in the if and elseif statements is false (see elseif ).

 

Elseif
Elseif is a combination of if and else, which is similar to the alias. 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 displays a is bigger than B, a equal to B, or a is smaller than B based on 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 ";
}
?>

 

Multiple elseif statements can exist in the same if structure. The elseif statement with the first expression value true (if any) will be executed. In php, you can also write "else if" (two words), which is identical to "elseif" (one word. The meaning of syntactic analysis is slightly different (if you are familiar with the c language, this is the same behavior), but the bottom line is that the two will produce the same behavior.

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


Else
It is often necessary to execute a statement when a condition is met, but other statements when the condition is not met. This is exactly the else function. Else extends the if statement and runs the statement when the expression value in the if statement is 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 ){
Print "a is bigger than B ";
} Else {
Print "a is not bigger than B ";
}
?>


The else statement is executed only when the value of the expression in the if and elseif statements is false (see elseif ).

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.