Process Control in PHP

Source: Internet
Author: User
Tags php script

Process Control in PHP:

Any PHP script is composed of a series of statements. A statement can be an assignment statement, a function call, a loop, a conditional statement, or even a statement that does nothing (an empty statement). The statement usually ends with a semicolon. You can also encapsulate a set of statements into a group of statements in curly braces. The statement province can be considered as a line statement.

If statement

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

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

If/else and Elseif/else if they two extend the IF statement.

While statement:

The while statement is the simplest looping statement in PHP. It behaves the same as the while in the C language. Its basic syntax is:

<? PHP  while (condition) {     statement};?

The meaning of the while statement is simple, and it tells PHP to TRUE repeat the nested loop statement as long as the value of the while expression is. The value of the expression is checked every time the loop is started, so even if the value changes in the Loop statement, the statement does not stop executing until the end of the loop. Sometimes a loop statement does not execute at the first FALSE time If the value of the while expression starts.

Do-while statement:

The do-while loop is very similar to the while loop, except that the value of the expression is checked at the end of each loop rather than at the beginning. The main difference from the normal while loop is that the do-while Loop statement is guaranteed to execute once (the true value of the expression is checked at the end of each loop ), but in general while Not necessarily in the loop (the expression truth value is checked at the beginning of the loop, and FALSE the entire loop terminates immediately if it starts). Different do-while loop usage, put the statement in the do-while(0), inside the loop with the break statement to end the execution loop.

For statement:

The For loop is the most complex loop structure in PHP. Its behavior is similar to the C language. The syntax for the For loop is:

 for (EXPR1; expr2; expr3)    Statement

The first expression ( expr1 ) is evaluated unconditionally (and executed) one time before the start of the loop.

expr2Evaluates before each cycle begins. If the value TRUE is, the loop continues and the nested loop statement is executed. If the value FALSE is, the loop is terminated.

expr3evaluated (and executed) after each loop.

Each expression can be empty or include multiple expressions separated by commas. In an expression expr2 , all expressions separated by commas are evaluated, but only the last result is taken. expr2null means an infinite loop (like C, which PHP secretly considers to TRUE be the value). This may not be as useless as you might think, because you often want to end a loop with a conditional break statement instead of using the FOR expression truth.

foreach () Statement:

The foreach syntax structure provides an easy way to iterate through an array. foreach can only be applied to arrays and objects, if you try to apply a variable to another data type, or an uninitialized variable will emit an error message. There are two kinds of syntax:

foreach  as $value )    statementforeachas$key$value)    statement

Switch statement:

A switch statement is similar to a series of if statements that have the same expression. In many cases it is necessary to compare the same variable (or expression) with many different values and execute different code depending on which value it equals. This is the purpose of the switch statement.

<? PHP Switch ($i) {    case 0 :        echo "I equals 0";          Break ;      Case 1:        echo "I equals 1";          Break ;      Case 2:        echo "I equals 2";          Break ;}? >  

Process Control in PHP

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