php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66238 Unexpected behavior in for loop
Submitted: 2013-12-05 10:15 UTC Modified: 2013-12-05 16:37 UTC
From: gnjagi at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2013-12-05 10:15 UTC] gnjagi at gmail dot com
Description:
------------
For loop with $i zero padded gives unexpected behaviour

Test script:
---------------
for($i=010;$i<20;$i++){
echo $i."<br/>";
}
echo "<br/>";
for($i=025;$i<50;$i++){
echo $i."<br/>";
}
echo "<br/>";
for($i=033;$i<60;$i++){
echo $i."<br/>";
}

Expected result:
----------------
The $i value is not what is stated in the for loop

Actual result:
--------------
http://softlogic.me.ke/numconv.php

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-05 12:17 UTC] phpmpan at mpan dot pl
Please stick to bug report template. What is the expected result?

As far as I can see (until I miss something) the linked (why linked instead of just providing the result?) output is exactly what is expected. There are three loops. First output values from 8 to 19, the second outputs values from 21 to 49 and the third: from 27 to 59. This is exactly what the code states and how it should work.
 [2013-12-05 12:27 UTC] gnjagii at gmail dot com
for($i=010;$i<20;$i++){
echo $i."<br/>";
}
expected is a loop with 10- 19

for($i=025;$i<50;$i++){
echo $i."<br/>";
}
expected is a loop with 25- 49

for($i=025;$i<50;$i++){
echo $i."<br/>";
}
expected is a loop with 33- 59

the actual result as you see in the link is something else
 [2013-12-05 13:30 UTC] gnjagi at gmail dot com
-Summary: Expected behavior in for loop +Summary: Unexpected behavior in for loop
 [2013-12-05 13:30 UTC] gnjagi at gmail dot com
Unexpected behavior in a for loop when a value is zero padded
 [2013-12-05 16:37 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2013-12-05 16:37 UTC] bwoebi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

010 is an octal notation, so parsed as 8.
025 in octal is 21.
033 in octal is 27.

See also http://www.php.net/manual/en/language.types.integer.php for the different integer notations.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Feb 25 08:01:27 2024 UTC