for

for루프는 PHP에서 제일 복잡한 루프이다. C와 똑같은 방식으로 동작한다. for루프의 문법은 다음과 같다:

for (expr1; expr2; expr3)
    statement

첫번째 표현식(expr1)은 루프의 시작에서 바로 조건없이 평가된다 (수행된다).

각 반복(iteration)의 시작부분에서 expr2이 평 가된다. 이 표현식이 TRUE이면 루프는 계속되고 내포된 구문(들)이 수행된다. FALSE이면, 루프 수행을 멈춘다.

expr3표현식은 각 반복의 끝부분에서 평가된다 (수행된다).

각 표현은 비어있거나 콤마로 구분한 여러 표현을 가질 수 있습니다. expr2에서, 콤마로 구분한 표현은 모두 평가되지만 결과는 마지막 부분에서만 가져옵니다. expr2이 비어있다는 것은 루프가 무제한 수행되어야 한다는 것을 의미한다 (PHP는 C처럼 TRUE로 인식) 이런 기법은 생각처럼 필요없지는 않다. 왜냐 하면 종종 for문의 표현식 대신에 break문으로 루프를 끝낼 필요가 있기 때문이다.

다음 예제 코드들을 보세요. 이 코드 모두 1부터 10까지의 숫자를 출력한다:

<?php
/* 예제 1 */

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

/* 예제 2 */

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

/* 예제 3 */

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

/* 예제 4 */

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

물론, 첫번째 예제(혹은 네번째) 코드가 가장 좋은 방법이다. 그러나 for루프에서 빈 표현식을 사용해야 하는 경우도 부딪히게 될것이다.

PHP는 for루프에 대한 대체 "콜른 문법"을 지원한다.

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

많은 사용자가 아래 예제처럼 배열을 탐색합니다.

<?php
/*
 * 이 배열은 루프를 도는 동안
 * 변경할 데이터를 가지고 있습니다.
 */
$people = Array(
        Array(
'name' => 'Kalle''salt' => 856412),
        Array(
'name' => 'Pierre''salt' => 215863),
        );

for(
$i 0$i sizeof($people); ++$i)
{
    
$people[$i]['salt'] = rand(000000999999);
}
?>

문제는 두번째 표현식입니다. 이 코드는 매 실행마다 배열의 크기를 계산하기 때문에 느려집니다. 크기는 변하지 않기 때문에, 크기를 저장하는 중간 변수를 사용하여 루프를 돌리도록 최적화 할 수 있습니다. 아래 예제가 보여줍니다:

<?php
$people 
= Array(
        Array(
'name' => 'Kalle''salt' => 856412),
        Array(
'name' => 'Pierre''salt' => 215863),
        );
 
for(
$i 0$size sizeof($people); $i $size; ++$i)
{
    
$people[$i]['salt'] = rand(000000999999);
}
?>

add a note add a note

User Contributed Notes 22 notes

up
295
matthiaz
12 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
52
nzamani at cyberworldz dot de
22 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
21
Andrew
10 years 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
Warbo
10 years 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
-1
mparsa1372 at gmail dot com
3 years ago
This example counts to 100 by tens:

<?php
for ($x = 0; $x <= 100; $x+=10) {
  echo
"The number is: $x <br>";
}
?>
up
-6
ju dot nk at email dot cz
6 years ago
Please note that following code is working:

for ($i=$reverse?($N-1):0; $reverse?($i>-1):($i<$N); $reverse?$i--:$i++) {

... your code here

}

(Using PHP 5.4.45)
up
-12
dx at e-mogensen dot dk
7 years ago
Warning about using the function "strlen" i a for-loop:

This note should might be under the "strlen" manual page, but there is a better chance for more paying attention here (nevertheless I have made a short note over there allso).

A loop function that test for the string length at each iteration takes forever (possibly due to "strlen" searches for the C-style string terminator - a binary 0 - every time..

So loops like this, using  "strlen" in the for...

for  ($i = 0;  $i < strlen($crc); $i++) .....

Will benefit tremendously in speed by a short step that saves the string length once and use that in the loop.

$clen = strlen($crc);

for  ($i = 0;  $i < $clen ; $i++) .....

Note: as a real hard-core programmer You are aware, that this is only valid if you don't change the string content inside the loop (and hereby allso the length). If the change is only occationly , You could just refresh the length variable or else just live with a quite slow loop.

This "discovery" was made from using an example of 16 bit crc calculation over at the "crc32" function manual page, that do exactly that..
up
-26
Vincenzo Raco
10 years 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
-16
AoKMiKeY
9 years 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
-22
Philipp Trommler
11 years 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
-16
htroyo
7 years ago
when iterating a multidimentional array like this:
for ($i = 0; $i < $size_x; $i++) {
    for ($j = 0; $j < $size_y; $j++) {
        do_something($a[$i][$j]);
    }
}
it is faster to use $a[$i][$j] than using $a[$j][$i]
for ($i = 0; $i < $size_x; $i++) {
    for ($j = 0; $j < $size_y; $j++) {
        do_something($a[$j][$i]);
    }
}
if you know about how RAM works you understand why
up
-15
JustinB at harvest dot org
18 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
-25
lishevita at yahoo dot co (notcom) .uk
17 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
-27
user at host dot com
20 years ago
Also acceptable:

<?php
 
for($letter = ord('a'); $letter <= ord('z'); $letter++)
   print
chr($letter);
?>
up
-8
ju dot nk at email dot cz
6 years ago
Please note that following code is working:

$reverse = TRUE;  //iteration direction switch

for ($i=$reverse?($N-1):0; $reverse?($i>-1):($i<$N); $reverse?$i--:$i++) {

... your code here

}

(Using PHP 5.4.45)
up
-7
Anonymous
3 years ago
If you want to do a for and increment with decimal numbers:
<?php
for($i=0; $i<=2; $i+=0.1)
     echo
$i;
?>
The code will never show 2 as expected, because of decimal imprecision if I remember well.
You will need to round:
<?php
for($i=0; round($i,1)<=2; $i+=0.1)
    echo
$i.",";
?>
This code correctly shows 0,0.1 .... 2.
up
-36
eduardofleury at uol dot com dot br
16 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
-33
kanirockz at gmail dot com
14 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
-25
bishop
20 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
-23
Anonymous
9 years 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
-15
epicxmoe at gmail dot com
6 years ago
Adding Letters from A to Z inside an array.

You should test it out.

<!DOCTYPE html>
<html>
<body>
<?php
$letter
= array();
for (
$letters = 'A'; $letters != 'AA'; $letters++)
{
   
array_push($letter, $letters);
}
echo
'<pre>' . var_export($letter, true) . '</pre>';
?>
</body>
</html>
up
-26
epicxmoe at gmail dot com
6 years ago
I've tried to search for a results on internet for a basic array which contain letters A to Z inside

<!DOCTYPE html>
<html>
<body>
<?php
$letter
= array();
for (
$letters = 'A'; $letters != 'AA'; $letters++)
{
   
array_push($letter, $letters);
}
echo
'<pre>' . var_export($letter, true) . '</pre>';
?>
</body>
</html>
To Top