Summarize the various PHP control statements _php Tutorial

Source: Internet
Author: User

PHP Control Statement 1, if statement

An If statement is an important feature in most languages, and it executes program segments based on conditions. The IF statement in PHP is similar to C:

    1. if (expr)
    2. Statement

As discussed in the expression, expr is computed as its true value. If expr is true, PHP executes the corresponding statement and, if False, ignores it.
If $ A is greater than $b, the following example shows ' A is bigger than B ':

    1. if ($a >$b)
    2. Print "A is bigger than B";

Typically, you want to execute more than one statement based on the condition. Of course, there is no need to add an IF judgment to each statement. Instead, multiple statements can be formed into a group of statements.

If statements can be nested within other if statements, allowing you to flexibly conditionally execute parts of the program.

PHP Control Statement 2, Else statement

Usually you want to execute a statement when a particular condition is met, and the condition is to execute another statement. else is used to do this. ELSE extended 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 display ' A is '

 
  
  
  1. Not bigger than B ':
  2. if ($a>$b) {
  3. Print "A is bigger than B";
  4. }
  5. else {
  6. Print "A is not bigger than B";
  7. }

PHP Control Statement 3, ElseIf statement

ELSEIF, as the name implies, is a combination of if and else, similar to else, that extends the IF statement to execute other statements when the IF expression is false. However, unlike else, it executes other statements only when the ElseIf expression is true.

You can use more than one ElseIf statement in an if statement. The first statement that ElseIf expression is true will be executed. In PHP 3, you can also write ' else if ' (written in two words) and ' ElseIf ' (written as a word) effect. This is just a small difference in writing (if you're familiar with C, it's also) and the result is exactly the same.
The ElseIf statement is executed only if the IF expression and any preceding ElseIf expression are false, and the current ElseIf expression is true.
The following is a nested-format if statement that contains ElseIf and else:

 
  
  
  1. if ($a==5):
  2. Print "a equals 5";
  3. Print "...";
  4. ElseIf ($a==6):
  5. Print "a equals 6";
  6. print "!!!";
  7. Else
  8. Print "A is neither 5 nor 6";
  9. endif

PHP Control Statement 4, while statement

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

while (expr) statement

The meaning of the while statement is very simple. It tells PHP to repeatedly execute nested statements as long as the while expression is true. The value of the while expression is checked each time the loop starts, so even if it changes its value within the nested statement, the execution does not terminate until the loop ends (each time PHP runs a nested statement called a loop). Similar to the 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 example is exactly the same, with the numbers 1 to 10:

/* Example 1 */

 
  
  
  1. $ I = 1 ;

The above loop executes only once, because after the first loop, when the truth expression is checked, it calculates that it is FALSE ($i not greater than 0) loop execution terminates.

PHP Control Statement 5, for Loop statement

The For loop is the most complex loop in PHP. The same as in C. The syntax for the For loop is:
for (EXPR1; expr2; expr3) statement
The first expression (EXPR1) is evaluated unconditionally (executed) at the beginning of the loop.
Each time the loop, the expression expr2 is computed. If the result is TRUE, the loop and nested statements continue to execute. If the result is FALSE, the entire loop ends.

At the end of each cycle, the EXPR3 is computed (executed). Each expression can be empty. Expr2 is empty, the number of cycles is variable (PHP defaults to True, like C). Don't do this unless you want to end the loop with a conditional break statement instead of a for truth expression.
Consider the following example. They all show numbers 1 through 10:

/* Example 1 */

 
  
  
  1. for ($ i = 1 ; $i) {
  2. break;
  3. }
  4. print $i;
  5. }
  6. /* Example 3 */
  7. $ i = 1 ;
  8. for (;;) {
  9. if ($i > ) {
  10. break;
  11. }
  12. print $i;
  13. $i + +;
  14. }

Of course, the first example is obviously the best, but you can see that you can use an empty expression for many occasions in the For loop.
Other languages have a foreach statement to traverse an array or hash table. PHP uses the While statement and the list (), each () function to achieve this function.

PHP Control Statement 6, switch SELECT statement

A switch statement is like a series of if statements on the same expression. In many ways, you want to compare the same variable (or expression) with many different values and execute different program segments based on different comparisons. This is the use of the switch statement.
The following two examples do the same thing in different ways, one with a set of if statements, and the other with a SWITCH statement:

/* Example 1 */

 
 
  1. if ($i = = 0) {
  2. print "I equals 0";
  3. }
  4. if ($i = = 1) {
  5. Print "I equals 1";
  6. }
  7. if ($i = = 2) {
  8. Print "I equals 2";
  9. }
  10. /* Example 2 */
  11. Switch ($i) {
  12. Case 0:
  13. print "I equals 0";
  14. Break
  15. Case 1:
  16. Print "I equals 1";
  17. Break
  18. Case 2:
  19. Print "I equals 2";
  20. Break
  21. }


http://www.bkjia.com/PHPjc/445943.html www.bkjia.com true http://www.bkjia.com/PHPjc/445943.html techarticle PHP Control Statement 1, if statement if statement is an important feature in most languages, it executes the program segment according to the condition. The IF statement in PHP is similar to c:if (expr) statement as discussed in the expression ...

  • 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.