ConFoo: Call for paper is now Open

for

(PHP 4, PHP 5)

for-Schleifen sind die komplexesten Schleifen in PHP. Sie verhalten sich wie ihre Pendants in C. Die Syntax einer for-Schleife ist:

for (expr1; expr2; expr3)
    statement

Der erste Ausdruck (expr1) wird vor Ausführung der Schleife ausgeführt.

Am Anfang jedes Schleifendurchlaufs wird die Anweisung expr2 ausgeführt. Wenn diese wahr ist (TRUE), wird die Schleife fortgesetzt und die untergeordneten Anweisungen werden ausgeführt. Andernfalls (FALSE) endet die Ausführung der Schleife.

Am Ende jedes Schleifendurchlaufs wird die Anweisung expr3 ausgeführt.

Jede der Anweisungen kann leer sein oder mehrere durch Kommata getrennte Anweisungen enthalten. In letzterem Fall werden bei expr2 zwar alle Anweisungen ausgeführt aber das Ergebnis wird nur von der letzten Anweisung verwendet. Wenn expr2 leer ist, läuft die Schleife unendlich lange (PHP nimmt dies als TRUE, ebenso wie C). Dies ist nicht so sinnlos, wie es scheint, denn oft will man die Schleife manuell mit break beenden.

Die folgenden Beispiele geben alle die Zahlen 1 bis 10 aus:

<?php
/* Beispiel 1 */

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

/* Beispiel 2 */

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

/* Beispiel 3 */

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

/* Beispiel 4 */

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

Sicher ist das erste das hübscheste (vielleicht auch das vierte), aber in manchen Fällen sind leere Anweisungen in for-Schleifen durchaus praktisch.

PHP untersützt bei for-Schleifen ebenfalls die alternative "Doppelpunkt-Syntax":

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

Es ist üblich, dass Arrays wie in dem folgenden Beispiel durchlaufen werden.

<?php
/*
 * Dies ist ein Array mit Daten, die wir in der
 * Schleife verändern wollen.
 */
$personen = array(
    array(
'name' => 'Hans''salt' => 856412),
    array(
'name' => 'Martin''salt' => 215863)
);

for(
$i 0$i count($personen); ++$i) {
    
$personen[$i]['salt'] = mt_rand(000000999999);
}
?>

Der obige Code kann langsam sein, weil die Größe des Arrays bei jedem Schleifendurchlauf abgerufen wird. Da sich die Größe nie ändern, kann die Schleife optimiert werden, indem man die Größe in einer Variablen zwischenspeichert, anstatt sie immer wieder mit count() abzurufen:

<?php
$personen 
= array(
    array(
'name' => 'Hans''salt' => 856412),
    array(
'name' => 'Martin''salt' => 215863)
);

for(
$i 0$groesse count($personen); $i $groesse; ++$i) {
    
$personen[$i]['salt'] = mt_rand(000000999999);
}
?>

add a note add a note

User Contributed Notes 16 notes

up
8
Vincenzo Raco
5 months ago
In this code:

<?php

    $array
= array(
       
'pop0',
       
'pop1',
       
'pop2',
       
'pop3',
       
'pop4',
       
'pop5',
       
'pop6',
       
'pop7',
       
'pop8'
   
);
    echo
"Tot Before: ".count($array)."<br><br>";
    for (
$i=0; $i<count($array); $i++) {
        if (
$i === 3) {
            unset(
$array[$i]);
        }
        echo
"Count: ".count($array). " - Position: ".$i."<br>";
    }
    echo
"<br> Tot After: ".count($array)."<br>";

?>

The result is:

---

Tot Before: 9

Count: 9 - Position: 0
Count: 9 - Position: 1
Count: 9 - Position: 2
Count: 8 - Position: 3
Count: 8 - Position: 4
Count: 8 - Position: 5
Count: 8 - Position: 6
Count: 8 - Position: 7

Tot After: 8

---

The position 8 is skipped, because the "expr2" {{ $i<count($array) }} is evaluated again, for each cycle.

The solution is:

<?php
   
    $array
= array(
       
'pop0',
       
'pop1',
       
'pop2',
       
'pop3',
       
'pop4',
       
'pop5',
       
'pop6',
       
'pop7',
       
'pop8'
   
);
    echo
"Tot Before: ".count($array)."<br><br>";
   
$count = count($array);
    for (
$i=0; $i<$count; $i++) {
        if (
$i === 3) {
            unset(
$array[$i]);
        }
        echo
"Count: ".count($array). " - Position: ".$i."<br>";
    }
    echo
"<br> Tot After: ".count($array)."<br>";
   
?>
up
9
nzamani at cyberworldz dot de
13 years ago
The point about the speed in loops is, that the middle and the last expression are executed EVERY time it loops.
So you should try to take everything that doesn't change out of the loop.
Often you use a function to check the maximum of times it should loop. Like here:

<?php
for ($i = 0; $i <= somewhat_calcMax(); $i++) {
 
somewhat_doSomethingWith($i);
}
?>

Faster would be:

<?php
$maxI
= somewhat_calcMax();
for (
$i = 0; $i <= $maxI; $i++) {
 
somewhat_doSomethingWith($i);
}
?>

And here a little trick:

<?php
$maxI
= somewhat_calcMax();
for (
$i = 0; $i <= $maxI; somewhat_doSomethingWith($i++)) ;
?>

The $i gets changed after the copy for the function (post-increment).
up
6
lishevita at yahoo dot co (notcom) .uk
8 years ago
On the combination problem again...

It seems to me like it would make more sense to go through systematically. That would take nested for loops, where each number was put through all of it's potentials sequentially.

The following would give you all of the potential combinations of a four-digit decimal combination, printed in a comma delimited format:

<?php
for($a=0;$a<10;$a++){
    for(
$b=0;$b<10;$b++){
          for(
$c=0;$c<10;$c++){
              for(
$d=0;$d<10;$d++){
                echo
$a.$b.$c.$d.", ";
              }
           }
      }
}
?>

Of course, if you know that the numbers you had used were in a smaller subset, you could just plunk your possible numbers into arrays $a, $b, $c, and $d and then do nested foreach loops as above.

- Elizabeth
up
6
matthiaz
2 years ago
Looping through letters is possible. I'm amazed at how few people know that.

for($col = 'R'; $col != 'AD'; $col++) {
    echo $col.' ';
}

returns: R S T U V W X Y Z AA AB AC

Take note that you can't use $col < 'AD'. It only works with !=
Very convenient when working with excel columns.
up
4
Andrew
8 months ago
You can use strtotime with for loops to loop through dates

<?php
for ($date = strtotime("2014-01-01"); $date < strtotime("2014-02-01"); $date = strtotime("+1 day", $date)) {
    echo
date("Y-m-d", $date)."<br />";
}
?>
up
4
eduardofleury at uol dot com dot br
7 years ago
<?php
//this is a different way to use the 'for'
//Essa é uma maneira diferente de usar o 'for'
for($i = $x = $z = 1; $i <= 10;$i++,$x+=2,$z=&$p){
   
   
$p = $i + $x;
   
    print
"\$i = $i , \$x = $x , \$z = $z <br />";
   
}

?>
up
2
user at host dot com
10 years ago
Also acceptable:

<?php
 
for($letter = ord('a'); $letter <= ord('z'); $letter++)
   print
chr($letter);
?>
up
0
Anonymous
23 days ago
You can also work with arrays. For example, say you want to generate an array of 12 unique 2-letter strings:

<?php

for ($names = array(); count($names) < 12; $names = array_unique($names)) {
   
/**
     * we assume here we have some $faker object
     * which generates n-letter strings
     */
   
$names[] = $faker->word(2);
}

print_r($names);
?>

will print something like:

Array
(
    [0] => cc
    [1] => cb
    [2] => dd
    [3] => db
    [4] => bb
    [6] => cd
    [8] => aa
    [9] => ad
    [10] => ca
    [11] => ac
    [12] => dc
    [15] => ab
)
up
0
vincentamorij at hotmail dot com
5 months ago
<html>
<head>
<title>
drikhoek

</title>
</head>
<body bgcolor="#99FF66">
<form action="driehoek.php" method="post" >
<input type="text" name="breedte"  />
<input type="submit" value="klikken"  />
</form>
<?

$hoogte=$_POST["breedte"];
print(" ");

for($teller=1; $teller<=$hoogte; $teller++)
{
    

    for($breedte=1; $breedte<=$teller; $breedte++)
       { print("*");
       }
print("   </br>");      


?>

it's an example!!!!!

</body>
</html>
up
-1
AoKMiKeY
2 months ago
As a note for people just starting out and wanting to know if you can do some thing like this...

<?php For( $a = 0; $a < 10; $a++ ) { ?>

//Random html elements you would like to duplicate.

<?php } ?>

Then yes you can. It works like a charm.
up
-2
Warbo
7 months ago
Remember that for-loops don't always need to go 'forwards'. For example, let's say I have the following code:

<?php
for ($i = 0; $i < calculateLoopLength(); $i++) {
 
doSomethingWith($i);
}
>?

As
other comments have pointed out, if "calculateLoopLength" will keep giving back the same value, it can be moved outside the loop:

<?
php
$loopLength
= calculateLoopLength();
for (
$i=0; $i < $loopLength; $i++) {
 
doSomethingWith($i);
}
?>

However, if the order the looping doesn't matter (ie. each iteration is independent) then we don't need to use an extra variable either, we can just count down (ie. loop 'backwards') instead:

<?php
for ($i=calculateLoopLength(); $i > 0; $i--) {
 
doSomething($i);
}
?>

In fact, we can simplify this even more, since "$i > 0" is equivalent to "$i" (due to type casting):

<?php
for ($i=calculateLoopLength(); $i; $i--) {
 
doSomething($i);
}
?>

Finally, we can switch to a 'pre-decrement' instead of a 'post-decrement' to be slightly more efficient (see, for example, http://dfox.me/2011/04/php-most-common-mistakes-part-2-using-post-increment-instead-of-pre-increment/ ):

<?php
for ($i = calculateLoopLength(); $i; --$i) {
 
doSomething($i);
}
?>

In this case we could also replace the entire loop with a map, which might make your algorithm clearer (although this won't work if calculateLoopLength() == 0):

<?php
array_map
('doSomething',
         
range(0, calculateLoopLength() - 1));
?>
up
-2
JustinB at harvest dot org
9 years ago
For those who are having issues with needing to evaluate multiple items in expression two, please note that it cannot be chained like expressions one and three can.  Although many have stated this fact, most have not stated that there is still a way to do this:

<?php
for($i = 0, $x = $nums['x_val'], $n = 15; ($i < 23 && $number != 24); $i++, $x + 5;) {
   
// Do Something with All Those Fun Numbers
}
?>
up
-8
Philipp Trommler
1 year ago
Note, that, because the first line is executed everytime, it is not only slow to put a function there, it can also lead to problems like:

<?php

$array
= array(0 => "a", 1 => "b", 2 => "c", 3 => "d");

for(
$i = 0; $i < count($array); $i++){

echo
$array[$i];

unset(
$array[$i]);

}

?>

This will only output the half of the elements, because the array is becoming shorter everytime the for-expression counts it.
up
-8
bishop
11 years ago
If you're already using the fastest algorithms you can find (on the order of O(1), O(n), or O(n log n)), and you're still worried about loop speed, unroll your loops using e.g., Duff's Device:

<?php
$n
= $ITERATIONS % 8;
while (
$n--) $val++;
$n = (int)($ITERATIONS / 8);
while (
$n--) {
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
}
?>

(This is a modified form of Duff's original device, because PHP doesn't understand the original's egregious syntax.)

That's algorithmically equivalent to the common form:

<?php
for ($i = 0; $i < $ITERATIONS; $i++) {
   
$val++;
}
?>

$val++ can be whatever operation you need to perform ITERATIONS number of times.

On my box, with no users, average run time across 100 samples with ITERATIONS = 10000000 (10 million) is:
Duff version:       7.9857 s
Obvious version: 27.608 s
up
-10
kanirockz at gmail dot com
4 years ago
Here is another simple example for " for loops"

<?php

$text
="Welcome to PHP";
$searchchar="e";
$count="0"; //zero

for($i="0"; $i<strlen($text); $i=$i+1){
   
    if(
substr($text,$i,1)==$searchchar){
   
      
$count=$count+1;
    }

}

echo
$count

?>

this will be count how many "e" characters in that text (Welcome to PHP)
up
-23
kanirockz at gmail dot com
4 years ago
Here is another simple example for " for loops"

<?php

$text
="Welcome to PHP";
$searchchar="e";
$count="0"; //zero

for($i="0"; $i<strlen($text); $i=$i+1){
   
    if(
substr($text,$i,1)==$searchchar){
   
      
$count=$count+1;
    }

}

echo
$count

?>

this will be count how many "e" characters in that text (Welcome to PHP)
To Top