|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-02-25 02:48 UTC] rjnewton at efn dot org
Description:
------------
Current PHP manual [Tue Nov 22 00:57:49 2005] suggests under Language Reference|Control Structures|while that while(0) keeps a loop in play until a break condition occurs. This is the reverse of the actual case.
Reproduce code:
---------------
<?php
$i = 12;
$factor = 0.5;
$minimum_limit = 2;
do {
if ($i < 5) {
echo "i is not big enough\n";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
break;
}
echo "i is ok\n";
/* process i */
} while (0);
?>
Expected result:
----------------
Should make two full passes through the loop, the exit at the first if statement of the third loop, since $i will then be 3, thus $ < 5 will evaluate true. This is the behavior when while(1) is used as the control condition.
Actual result:
--------------
using while(0):
D:\newtonj\PHPstuff>php hello.php
i is ok
using while(1):
D:\newtonj\PHPstuff>php hello.php
i is ok
i is ok
i is not big enough
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 05:00:01 2025 UTC |
do { /* this code will be executed once */ } while (0); I wonder where you saw that while(0) will produce an endless loop until break;. Using a do-while(0) allows you to break it at the middle of the block, that's all. anyway: "Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this 'feature'."The manual is correct. Your statement is bogus, even with the corrections. do { /* code in the block*/ } while(0); executes the code in the block *once* as the condition is evaluated *after* the first loop. The only advantage you have is that you can use "break" to skip the rest of the block. So: /* some code */ if ($var === true) { /* some other code */ } could be written do { /* some code */ if ($var !== true) { break; } /* some other code */ } while(0); This behaviour is explained correctly in the manual, so please let this bug report closed.