php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71262 for loop error
Submitted: 2016-01-02 02:21 UTC Modified: 2016-01-02 07:06 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: m dot dziczkowski at prokonto dot pl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.4 OS: Linux
Private report: No CVE-ID: None
 [2016-01-02 02:21 UTC] m dot dziczkowski at prokonto dot pl
Description:
------------
I had found a bug in the for loop. Increasing value is working, but decreasing isn't. See code


 

Test script:
---------------
// Working
for ($y = 1945; $y <= 2015; $y++)
{
  // some code to do
}

// Not working
for ($y = 2015; $y <= 1945; $y--)
{
  // some code to do
}

// sames happens if I replace (in the not working one) the "2015" with a variable (for example: $year = 2015; or $year = date("Y");

Expected result:
----------------
displayed values from 2015 to 1945

Actual result:
--------------
none

Patches

none (last revision 2016-01-02 02:23 UTC by m dot dziczkowski at prokonto dot pl)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-02 02:27 UTC] stas@php.net
-Status: Open +Status: Feedback -Package: PHP Language Specification +Package: Scripting Engine problem
 [2016-01-02 02:27 UTC] stas@php.net
Please provide full code, expected and actual output and explain what you mean by "not working".
 [2016-01-02 06:59 UTC] m dot dziczkowski at prokonto dot pl
-Status: Feedback +Status: Open -PHP Version: Irrelevant +PHP Version: 5.6.4
 [2016-01-02 06:59 UTC] m dot dziczkowski at prokonto dot pl
/* The select bellow don't shows other options then "-- select --" */

// code trunchated for securuty reasons

<select name = "year">
<option value = "#">-- Select --</option>
<?
for ($y = 2015; $y <= 1945; $y--)
{
   echo "<option value = \"$y\">$y</option>";
}
?>
</select>

/* When I done a small change into: */

<select name = "year">
<option value = "#">-- Select --</option>
<?
for ($y = 1945 $y <= 2015; $y++)
{
   echo "<option value = \"$y\">$y</option>";
}
?>
</select>

/* the code started to work */
 [2016-01-02 07:06 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-01-02 07:06 UTC] requinix@php.net
> for ($y = 2015; $y <= 1945; $y--)
$y starts at 2015. 2015 <= 1945 is false. Loop exits immediately.

You want
> for ($y = 2015; $y >= 1945; $y--)
 [2016-01-08 11:11 UTC] php at duncanc dot co dot uk
Your example is flawed, the second "not working" loop will not loop because $y is never "less than or equal to" 1945, it starts off at 2015. I think you meant to do:

for ($y = 2015; $y >= 1945; $y--)
{
  // some code to do
}
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 19:00:01 2026 UTC