php教程

while

(PHP 4, PHP 5, PHP 7)

while 循环是 PHP 中最简单的循环类型。它和 C 语言中的 while 表现地一样。while 语句的基本格式是:

while (expr)
    statement

while 语句的含意很简单,它告诉 PHP 只要 while 表达式的值为 TRUE 就重复执行嵌套中的循环语句。表达式的值在每次开始循环时检查,所以即使这个值在循环语句中改变了,语句也不会停止执行,直到本次循环结束。有时候如果 while 表达式的值一开始就是 FALSE,则循环语句一次都不会执行。

if 语句一样,可以在 while 循环中用花括号括起一个语句组,或者用替代语法:

while (expr):
    statement
    ...
endwhile;

下面两个例子完全一样,都显示数字 1 到 10:

<?php
/* example 1 */

$i 1;
while (
$i <= 10) {
    echo 
$i++;  /* the printed value would be
                    $i before the increment
                    (post-increment) */
}

/* example 2 */

$i 1;
while (
$i <= 10):
    print 
$i;
    
$i++;
endwhile;
?>

User Contributed Notes

mattbacq2004 at gmail dot com 03-Oct-2018 04:10
<?php

$i
= 50; // number of loops

/*

the variable can
be modified:              $i

*/

while(!$i ==0){
   
DoAnyFunction();
}
nickleus at gmail dot com 29-Nov-2017 03:34
<?php
$i
= -1;
while (
$i) {
    echo
$i++;
}
?>
outputs  "-1" then stops because "0" (zero) gets evaluated as FALSE.

this demonstrates why it's important for a PDO statement fetch-ing a column value inside a while-loop to test explicitly for FALSE.
Niton 04-Mar-2017 02:26
@stuart.
You need to use reset($two), because your array index already left the last element and you need to reset it. Outer while iterates again but inner while executes nothing.
synnus at gmail dot com 28-Apr-2016 05:35
<?php

// test While Vs For php 5.6.17

$t1 = microtime(true);
$a=0;
while(
$a++ <= 1000000000);
$t2 = microtime(true);
$x1 = $t2 - $t1;
echo
PHP_EOL,' > while($a++ <= 100000000); : ' ,$x1, 's', PHP_EOL;

$t3 = microtime(true);
for(
$a=0;$a <= 1000000000;$a++);
$t4 = microtime(true);
$x2 = $t4 - $t3;
echo
PHP_EOL,'> for($a=0;$a <= 100000000;$a++); : ' ,$x2, 's', PHP_EOL;

$t5 = microtime(true);
$a=0; for(;$a++ <= 1000000000;);
$t6 = microtime(true);
$x3 = $t6 - $t5;
echo
PHP_EOL,' > $a=0; for(;$a++ <= 100000000;); : ' , $x3, 's', PHP_EOL;

//> while($a++ <= 100000000);   = 18.509671926498s
//
//> for($a=0;$a <= 100000000;$a++);  =  25.450572013855s
//
//> $a=0; for(;$a++ <= 100000000;);  =  22.614907979965s

// ===================

//> while($a++ != 100000000); : 18.204656839371s
//
//> for($a=0;$a != 100000000;$a++); : 25.025605201721s
//
//> $a=0; for(;$a++ != 100000000;); : 22.340576887131s

// ===================

//> while($a++ < 100000000); : 18.383454084396s
//
//> for($a=0;$a < 100000000;$a++); : 25.290743112564s
//
//> $a=0; for(;$a++ < 100000000;); : 23.28609919548s

?>
er dot sarimkhan786 at gmail dot com 05-Oct-2015 08:34
simple pyramid pattern program using while loop
<?php
$i
=1;
while(
$i<=5)
{
   
$j=1;
    while(
$j<=$i)
    {
      echo
"*&nbsp&nbsp";
     
$j++;     
    }
    echo
"<br>";
   
$i++;
}
?>
// or alternatively you can use:
<?php
$i
=1;
while(
$i<=5):

   
$j=1;
    while(
$j<=$i):
      echo
"*&nbsp&nbsp";
     
$j++;     
    endwhile;
   
    echo
"<br>";
   
$i++;
endwhile;
?>
Anonymous 23-Apr-2015 05:56
A cool way to keep evaluating something until it fails a test.

<?php
while (true) {
  if (
'test') { // is initial condition true
    // do something that also changes initial condition
 
} else { // condition failed
   
break; // leave loop
 
}
}
?>
avenidagez at foro5 dot com 03-Dec-2014 07:55
Is strange that the manual states...
 "Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once. "

Because it can't be SOMETIMES

If it behaves that way, then it is a bug, because it ALWAYS must not run the nested statement(s) even once if the WHILE expression evaluates to FALSE from the very beginning.

Another way to exit the while loop is by using the BREAK statement.. see it in the manual.

And if expression evaluates to NULL is the same as FALSE
while (expression evals to NULL){ }
qeremy [atta] gmail [dotta] com 22-Aug-2012 08:45
Instead of this usage;

<?php
$arr
= array("orange", "banana", "apple", "raspberry");

$i = 0;
while (
$i < count($arr)) {
  
$a = $arr[$i];
   echo
$a ."\n";
  
$i++;
}
// or
$i = 0;
$c = count($arr);
while (
$i < $c) {
  
$a = $arr[$i];
   echo
$a ."\n";
  
$i++;
}
?>

This could be more efficient;

<?php
while ($a = $arr[1 * $i++]) echo $a ."\n";
?>
ravenswd at gmail dot com 06-Mar-2010 07:34
I find it often clearer to set a simple flag ($finished) to false at the start of the loop, and have the program set it to true when it's finished doing whatever it's trying to do. Then the code is more self-documenting: WHILE NOT FINISHED keep going through the loop. FINISHED EQUALS TRUE when you're done. Here's an example. This is the code I use to generate a random filename and ensure that there is not already an existing file with the same name. I've added very verbose comments to it to make it clear how it works:

<?php
$finaldir
= 'download';

$finished = false;                       // we're not finished yet (we just started)
while ( ! $finished ):                   // while not finished
 
$rn = rand();                          // random number
 
$outfile = $finaldir.'/'.$rn.'.gif';   // output file name
 
if ( ! file_exists($outfile) ):        // if file DOES NOT exist...
   
$finished = true;                    // ...we are finished
 
endif;
endwhile;                               
// (if not finished, re-start WHILE loop)
?>
scott at mstech dot com 06-Oct-2009 02:48
Just a note about using the continue statement to forego the remainder of a loop - be SURE you're not issuing the continue statement from within a SWITCH case - doing so will not continue the while loop, but rather the switch statement itself.

While that may seem obvious to some, it took a little bit of testing for me, so hopefully this helps someone else.
s dot seitz at netz-haut dot de 24-Jun-2008 08:21
Due to the fact that php only interprets the necessary elements to get a result, I found it convenient to concatenate different sql queries into one statement:

<?php

$q1
= 'some query on a set of tables';
$q2 = 'similar query on a another set of tables';

if ( (
$r1=mysql_query($q1)) && ($r2=mysql_query($q2)) ) {

     while ((
$row=mysql_fetch_assoc($r1))||($row=mysql_fetch_assoc($r2))) {

        
/* do something with $row coming from $r1 and $r2 */

     
}
   }

?>

[EDIT BY danbrown AT php DOT net: Contains a bugfix supplied by "Ira" on 14-AUG-09 to address an extra '(' in the leading `if` statement.]
dominik at deobald dot org 23-Feb-2007 08:50
@stuart:

There's nothing strange or unexpected about your loop's behaviour.

> So in effect the main while loop is only doing one iteration... and not 4 as expected....

That's the wrong conclusion. The outer "while" does all four iterations. However the "inner" loop does nothing for the second, third and fourth run.

> I think it would be good to have an explaination of this strange behaviour.

Here it is:

<?PHP
$i
=0;
while(
$i < count($one)) {
  
   while(
$a = each($two)) {
       echo
$a[1]." - ".$one[$i].", ";
   }
  
$i++;
  
}
?>

The "problem" is your use of "each", which reached the last item after the first iteration of the outer loop. After that, when you come back to the second iteration with the outer loop, "each" still is at the end of the array $two.

If you add a reset($two) in front of the inner "while", you'll get the result you expect.
chris mushy 11-May-2005 06:43
Just a note to stuart - the reason for this behaviour is because using the while(value = each(array)) construct increments the internal counter of the array as its looped through. Therefore if you intend to repeat the loop, you need to reset the counter. eg:

$one = array("10", "20", "30", "40");
$two = array("a", "b", "c", "d");

$i=0;
while($i < count($one)) {
   reset($two);
   while($a = each($two)) {
       echo $a[1]." - ".$one[$i].", ";
   }
   $i++;
  
}

This produces:

a - 10, b - 10, c - 10, d - 10, a - 20, b - 20, c - 20, d - 20, a - 30, b - 30, c - 30, d - 30, a - 40, b - 40, c - 40, d - 40,
stuart 11-May-2005 02:06
A note to anyone nesting a while loop inside a while loop....

Consider the example below:

$one = array("10", "20", "30", "40");
$two = array("a", "b", "c", "d");

$i=0;
while($i < count($one)) {
   
    while($a = each($two)) {
        echo $a[1]." - ".$one[$i].", ";
    }
    $i++;
   
}

This will return the following:
a - 10, b - 10, c - 10, d - 10

So in effect the main while loop is only doing one iteration... and not 4 as expected....

Now the example below works as expected..
$i=0;
while($i < count($one)) {
   
    foreach($two as $a) {
        echo $a." - ".$one[$i]."\n";
    }
    $i++;
   
}

by returning:
a - 10, b - 10, c - 10, d - 10, a - 20, b - 20, c - 20, d - 20, a - 30, b - 30, c - 30, d - 30, a - 40, b - 40, c - 40, d - 40

So there is clearly a difference on how while statements work in comparison to other looping structures.

I think it would be good to have an explaination of this strange behaviour.
13-Mar-2005 09:54
virtualjosh at yahoo dot com (Hosh) wrote on: 16-Aug-2003 12:52

The speedtest is interesting. But the seemingly fastest way contains a pitfall for beginners who just use it because it is fast and fast is cool ;)

Walking through an array with next() will cut of the first entry, as this is the way next() works ;)

If you really need to do it this way, make sure your array contains an empty entry at the beginning. Another way would be to use

<?php
while ($this = current($array) ){
   
do_something($this);
   
next($array);
}
?>

There is an impact on speed for sure but I did not test it. I would advise to stick with conventional methods because current(),next() in while loops is too error prone for me.
Ilene Jones 21-Feb-2005 01:12
For Perl programmers, break is similar to last

while (1) {
   while(cond) {
       if (error) {
           break 2; // in perl this could have been last;
       }
   }
}
corychristison[AT]NSPAMlavacube[dot]com 03-Dec-2004 03:08
While can do wonders if you need something to queue writing to a file while something else has access to it.

Here is my simple example:

<?php

 
function write ($data, $file, $write_mode="w") {
   
$lock = $file . ".lock";
    
// run the write fix, to stop any clashes that may occur
   
write_fix($lock);
    
// create a new lock file after write_fix() for this writing session
   
touch( $lock );
    
// write to your file
   
$open = fopen($file, $write_mode);
   
fwrite($open, $data);
   
fclose($open);
    
// kill your current lock
   
unlink($lock);
  }

  function
write_fix ($lock_file) {
    while(
file_exists($lock_file){
     
// do something in here?
      // maybe sleep for a few microseconds
      // to maintain stability, if this is going to
      // take a while ?? [just a suggestion]
   
}
  }

?>

This method is not recommended for use with programs that will be needing a good few seconds to write to a file, as the while function will eat up alot of process cycles.  However, this method does work, and is easy to implement.  It also groups the writing functions into one easy to use function, making life easier. :-)
virtualjosh at yahoo dot com (Hosh) 15-Aug-2003 03:52
I made a test traversing an array (simple, but long, numeric array with numeric keys). My test had a cycle per method, and multiplied each array element by 100.. These were my results:

******************************************************
30870 Element Array Traversing

[test_time] [BEGINS/RESETS @ time_start = 1060977996.689]
0.2373 seg later -> while (list ($key, $val) = each ($array)) ENDS

[test_time] [BEGINS/RESETS @ time_start = 1060977996.9414]
0.1916 seg later -> while (list ($key,) = each ($array))  ENDS

[test_time] [BEGINS/RESETS @ time_start = 1060977997.1513]
0.1714 seg later -> foreach ($array AS $key=>$value) ENDS

[test_time] [BEGINS/RESETS @ time_start = 1060977997.3378]
0.0255 seg later -> while ($next = next($array)) ENDS

[test_time] [BEGINS/RESETS @ time_start = 1060977997.3771]
0.1735 seg later -> foreach ($array AS $value) ENDS
**************************************************************

foreach is fatser than a while (list  - each), true.
However, a while(next) was faster than foreach.

These were the winning codes:

$array = $save;
test_time("",1);
foreach ($array AS $key=>$value)
    $array[$key] = $array[$key] * 100;
test_time("foreach (\$array AS \$key=>\$value)");

$array = $save;
test_time("",1);
reset($array);
while ($next = next($array))
{    $key = key($array);
    $array[$key] = $array[$key] * 100;
}       
test_time("while (\$next = next(\$array))");
*********************************************************
The improvement seems huge, but it isnt that dramatic in real practice. Results varied... I have a very long bidimensional array, and saw no more than a 2 sec diference, but on 140+ second scripts.  Notice though that you lose control of the $key  value (unless you have numeric keys, which I tend to avoid), but it is not always necessary. 

I generally stick to foreach. However, this time, I was getting Allowed Memory Size Exceeded errors with Apache. Remember foreach copies the original array, so this now makes two huge 2D arrays in memory and alot of work for Apache. If you are getting this error, check your loops. Dont use the whole array on a foreach. Instead get the keys and acces the cells directlly. Also, try and use unset and Referencing on the huge arrays.

Working on your array and loops is a much better workaround than saving to temporary tables and unsetting (much slower).
Merve 10-Jul-2003 09:49
This is an easy way for all you calculator creators to make it do factorials. The code is this:

<?php
$c
= ($a-1);
$d = $a;
while (
$c>=1)
{
$a = ($a*$c);
$c--;
}
print (
" $d! = $a");
?>

$a changes, and so does c, so we have to make a new variable, $d, for the end statement.
chayes at antenna dot nl 26-Feb-2002 02:42
At the end of the while (list / each) loop the array pointer will be at the end.
This means the second while loop on that array will be skipped!

You can put the array pointer back with the reset($myArray) function.

example:

<?php
$myArray
=array('aa','bb','cc','dd');
 while (list (
$key, $val) = each ($myArray) ) echo $val;
reset($myArray);
 while (list (
$key, $val) = each ($myArray) ) echo $val;
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788