Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

for

for loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a for loop is:

for (expr1; expr2; expr3)
    statement

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, expr3 is evaluated (executed).

Each of the expressions can be empty or contain multiple expressions separated by commas. Comma separated expressions in expr2 are treated similarly to being separated by the || operator but has a lower precedence than ||. expr2 being empty means the loop should be run indefinitely (PHP implicitly considers it as TRUE, like C). This may not be as useless as you might think, since often you'd want to end the loop using a conditional break statement instead of using the for truth expression.

Consider the following examples. All of them display numbers from 1 to 10:

<?php
/* example 1 */

for ($i = 1; $i <= 10; $i++) {
    echo
$i;
}

/* example 2 */

for ($i = 1; ; $i++) {
    if (
$i > 10) {
        break;
    }
    echo
$i;
}

/* example 3 */

$i = 1;
for (; ; ) {
    if (
$i > 10) {
        break;
    }
    echo
$i;
   
$i++;
}

/* example 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>

Of course, the first example appears to be the nicest one (or perhaps the fourth), but you may find that being able to use empty expressions in for loops comes in handy in many occasions.

PHP also supports the alternate "colon syntax" for for loops.

for (expr1; expr2; expr3):
    statement
    ...
endfor;



foreach> <do-while
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
for
jdstraughan at gmail dot com
14-Sep-2007 07:49
Make date drop down in form default to today's date:

<html><body><form>

<?php

$dateMMs
= date("m");
$dateDDs = date("d");
$dateYYs = date("Y");
//$date = $dateYYs.'-'.$dateMMs.'-'.$dateDDs;

//Make dateMM Dropdown
echo '<select name="dateMM">';
for (
$i=01; $i<=12; $i++) {
   
$sel = '';
    if (
$dateMMs == $i) {$sel=' selected ';}
    echo
'<option value="'.$i.'" '.$sel.' >'.$i.'</option>';
    }
echo
'</select>';

echo
'/';

//Make dateDD Dropdown
echo '<select name="dateDD">';
for (
$i=1; $i<=31; $i++) {
   
$sel = '';
    if (
$dateDDs == $i) {$sel=' selected ';}
    echo
'<option value="'.$i.'" '.$sel.' >'.$i.'</option>';
    }
echo
'</select>';

echo
'/';

//Make dateYY Dropdown
echo '<select name="dateYY">';
for (
$i=2007; $i<=2010; $i++) {
   
$sel = '';
    if (
$dateYYs == $i) {$sel=' selected ';}
    echo
'<option value="'.$i.'" '.$sel.' >'.$i.'</option>';
    }
echo
'</select>';

?>

</form></body></html>
david dot sledge at yahoo dot com
13-Sep-2007 03:11
In response to mused.biz's comment:

> Incrementing two variables together can also be achieved, eg:
>
> for ($j = "a", $k = "1"; $j <= "f", $k <= "6" ; $j++, $k++)

Note that the statement:

    $j <= "f"

has absolutely no effect on this loop (which isn't to say it doesn't get evaluated), because it is separated from the following statement by a comma.  If $j were to exceed the value "f" before $k exceeded "6", the loop would still continue.

However, the statement:

<?php

for ( $i = 0; $i++, $i < 10; $i++ )
    echo
$i . '<br/>';

?>

will result in the following:

1
3
5
7
9

because $i is incremented before the condition is evaluated, in addition to it being incremented after the completion of each loop.
eduardofleury at uol dot com dot br
14-Jun-2007 06:18
<?php
//this is a different way to use the 'for'
//Essa
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com
Z058440144362 Z348613067571