Control statements in PHP

Source: Internet
Author: User

1, if statement

An If statement is an important feature in most languages, and it executes the program segment according to the conditions. PHP's If statement is similar to C:

if (expr)

Statement


As discussed in an expression, expr is computed as its true value. If expr is true, PHP executes the corresponding statement and ignores it if false.

If $a is greater than $b, the following example displays ' A is bigger than B ':

if ($a > $b)

Print "A is bigger than B";


Typically, you want to execute more than one statement based on a condition. Of course, you do not need to add an IF to each statement. Instead, you can compose multiple statements into a single statement group.
If statements can be nested within other if statements, allowing you to flexibly conditionally execute parts of the program.

2. Else statement

Usually you want to execute a statement when a particular condition is met, and the condition is executed by another statement. else is used to do this. Else expands the IF statement to execute another statement when the IF statement expression is false. For example, the following program executes if $a is greater than $b displays ' A is bigger than B ', otherwise displays ' A is not bigger than B ':

if ($a > $b) {
Print "A is bigger than B";
}
else {
Print "A is not bigger than B";
}



3, ElseIf statement

ELSEIF, as the name shows, is a combination of if and else, similar to else, which extends the IF statement to execute other statements when the IF expression is false. Unlike else, however, it executes other statements only when the ElseIf expression is also true.

You can use more than one ElseIf statement in an if statement. A statement with the first ElseIf expression of true will be executed. In PHP 3, you can also write ' else if ' (written in two words) and ' ElseIf ' (write a word) effect. This is just a small difference in writing (if you are familiar with C, it is), the result is exactly the same.

The ElseIf statement executes only if the IF expression and any previous ElseIf expression are false, and the current ElseIf expression is true.
The following is an if statement with a nested format containing ElseIf and else:

if ($a ==5):
Print "a equals 5";
Print "...";
ElseIf ($a ==6):
Print "a equals 6";
print "!!!";
Else
Print "A is neither 5 nor 6";
endif


 

4, while statement

The while loop is a simple loop of PHP 3. The same as in C. The basic format of the while statement is:

while (expr) statement

The while statement has a very simple meaning. It tells PHP to repeatedly execute nested statements as long as the while expression is true. The value of the while expression is checked at the beginning of each loop, so even if its value is changed within the nested statement, this execution will not terminate until the loop ends (each time PHP runs a nested statement called a loop). Like an If statement, you can enclose a set of statements in curly braces and execute multiple statements in the same while loop:

while (expr): statement ... Endwhile;

The following examples are exactly the same, with numbers 1 to 10 being played:

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.