PHP4 User Manual: Process Control -> for

zhaozj2021-02-08  216

for

The FOR cycle is a very complex cycle in PHP. Its behavior is like a C language. The following is the syntax of the loop:

For (expr1; expr2; expr3) statement

The first expression (expr1) is a value that is unconditionally starting.

At each time, the expression expr2 is calculated. If the value is True, continue loop and perform the nested statement. If the value is false, after the loop is executed

Each time it is repeated, the expression EXPR3 is calculated (executed).

Each expression can be empty. EXPR2 means that the loop will run uncertain operation (PHP is dim, it is true, like C language). This is not likely to end as you think, you can use the Break statement to end the loop instead of for fact expressions.

Consider the following examples. They will display numbers 1 to 10:

/* example 1 */

For ($ I = 1; $ i <= 10; $ i ) {

Print $ i;

}

/ * Example 2 * /

For ($ i = 1; $ i ) {

IF ($ I> 10) {

Break;

}

Print $ i;

}

/ * Example 3 * /

$ i = 1;

For (;;) {

IF ($ I> 10) {

Break;

}

Print $ I;

$ i ;

}

/ * Example 4 * /

For ($ I = 1; $ I <= 10; Print $ I, $ I );

Of course, it is seen from the first example that is a brief (or fourth), but you can find a lot of opportunities you can use empty expressions.

PHP still supports the "colon syntax".

FOR (expr1; expr2; expr3): statement; ...;

Another my language has a Foreach statement to operate an array or hash. There is no such structure in PHP 3; PHP 4 has (see foreach). In PHP 3, people can use the while, list () and each () functions to complete the same function. See these functions.

转载请注明原文地址:https://www.9cbs.com/read-3440.html

New Post(0)