文字

for

(PHP 4, PHP 5)

for 循环是 PHP 中最复杂的循环结构。它的行为和 C 语言的相似。 for 循环的语法是:

for (expr1; expr2; expr3)
    statement

第一个表达式( expr1 )在循环开始前无条件求值(并执行)一次。

expr2 在每次循环开始前求值。如果值为 TRUE ,则继续循环,执行嵌套的循环语句。如果值为 FALSE ,则终止循环。

expr3 在每次循环之后被求值(并执行)。

每个表达式都可以为空或包括逗号分隔的多个表达式。表达式 expr2 中,所有用逗号分隔的表达式都会计算,但只取最后一个结果。 expr2 为空意味着将无限循环下去(和 C 一样,PHP 暗中认为其值为 TRUE )。这可能不像想象中那样没有用,因为经常会希望用有条件的 break 语句来结束循环而不是用 for 的表达式真值判断。

考虑以下的例子,它们都显示数字 1 到 10:

<?php


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



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



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



for ( $i  1 $j  0 $i  <=  10 $j  +=  $i , 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  count ( $people ); ++ $i )
{
    
$people [ $i ][ 'salt' ] =  rand ( 000000 999999 );
}
?>

以上代码可能执行很慢,因为每次循环时都要计算一遍数组的长度。由于数组的长度始终不变,可以用一个中间变量来储存数组长度以优化而不是不停调用 count()

<?php
$people 
= Array(
        Array(
'name'  =>  'Kalle' 'salt'  =>  856412 ), 
        Array(
'name'  =>  'Pierre' 'salt'  =>  215863 )
        );

for(
$i  0 $size  count ( $people );  $i  $size ; ++ $i )
{
    
$people [ $i ][ 'salt' ] =  rand ( 000000 999999 );
}
?>

用户评论:

[#1] Anonymous [2014-08-23 11:39:52]

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)) {
    

    
$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
)

[#2] AoKMiKeY [2014-07-13 18:36:11]

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.

[#3] vincentamorij at hotmail dot com [2014-04-15 14:22:21]

<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>
<?php

$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>

[#4] Vincenzo Raco [2014-03-30 16:59:09]

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>";
    
?>

[#5] Warbo [2014-02-06 17:37:36]

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 valueit 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(0calculateLoopLength() - 1));
?>

[#6] Andrew [2014-01-15 11:07:05]

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 />";
}
?>

[#7] Philipp Trommler [2012-11-08 16:34:31]

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(=> "a"=> "b"=> "c"=> "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.

[#8] matthiaz [2012-02-08 14:37:39]

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.

[#9] kanirockz at gmail dot com [2010-03-21 11:49:03]

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)

[#10] kanirockz at gmail dot com [2010-03-21 11:48:23]

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)

[#11] eduardofleury at uol dot com dot br [2007-06-14 06:18:13]

<?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 />";
    
}

?>

[#12] lishevita at yahoo dot co (notcom) .uk [2006-09-08 12:33:46]

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

[#13] JustinB at harvest dot org [2005-08-04 16:23:13]

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
}
?>

[#14] user at host dot com [2004-04-19 03:53:24]

Also acceptable:

<?php
  
for($letter ord('a'); $letter <= ord('z'); $letter++)
   print 
chr($letter);
?>

[#15] bishop [2003-07-17 13:23:29]

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

[#16] nzamani at cyberworldz dot de [2001-06-17 23:47:33]

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 <= $maxIsomewhat_doSomethingWith($i++)) ;
?>


The $i gets changed after the copy for the function (post-increment).

上一篇: 下一篇: