php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #36523 Implies That 0 Evaluates True
Submitted: 2006-02-25 02:48 UTC Modified: 2006-02-26 20:27 UTC
From: rjnewton at efn dot org Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: rjnewton at efn dot org
New email:
PHP Version: OS:

 

 [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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-25 03:01 UTC] rjnewton at efn dot org
Documentation reference should have been Language Reference|Control Structures|do-while rather than Language
Reference|Control Structures|while.

Also, "thus $ < 5 will evaluate true." Should have been "thus $i < 5 will evaluate true." in the Expected Results section.
Sorry,

Joseph
 [2006-02-25 12:47 UTC] colder@php.net
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'."
 [2006-02-25 23:19 UTC] rjnewton at efn dot org
"Advanced C users may be familiar with a different usage of the do-while loop, to allow stopping execution in the middle of code blocks, by encapsulating them with do-while (0), and using the break statement. The following code fragment demonstrates this: "

The above appeared in the PHP Manual (php_manual_en.chm) downloaded within the past five days from this site.  The location within the manual is provided in my correction of the original post.

Yes, of course this is bogus.  That is why I am making an effort to get it corrected.  Please read and comprehend before commenting in the future.
 [2006-02-25 23:41 UTC] nlopess@php.net
The code is right.
 [2006-02-26 20:27 UTC] colder@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 09:01:33 2024 UTC