php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10031 Stange behaviour in for loop with negative values
Submitted: 2001-03-27 17:15 UTC Modified: 2001-03-29 06:55 UTC
From: php at suares dot nl Assigned:
Status: Closed Package: Variables related
PHP Version: 4.0.3pl1 OS: Debian Linux 2.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
32 - 22 = ?
Subscribe to this entry?

 
 [2001-03-27 17:15 UTC] php at suares dot nl
Hi,

I tried to do something which was not really weird, but the the output astounded me. I tried to get opinions of two of my friends on this, but they didn't really help. I feel really stupid, because this looks much more like one of my programming errors then a bug.

I tried to execute this script:

<?
echo "<H1>Start Day = $start_day,View_Days=$view_days</H1>";
for ($itchy = $start_day; $itchy <= ($start_day+$view_days);$itchy++) {

$this_mktime=mktime(0,0,0,date("m"),date("d")+$itchy,date("Y"));

echo date("D j M",$this_mktime)."<BR>\n";
}
?>

I saved it in a file called test.php and called it from a website. like this:

www.yoursite/test.php?start_day=4&view_days=10

The output was, as expected:

Start Day = 4, View_Days= 10
Sun 1 Apr
Mon 2 Apr
Tue 3 Apr
Wed 4 Apr
Thu 5 Apr
Fri 6 Apr
Sat 7 Apr
Sun 8 Apr
Mon 9 Apr
Tue 10 Apr
Wed 11 Apr

(Ok, it shows 11 days, but what the heck).

Now, I tried to do this with some date in the past:

www.yoursite/test.php?start_day=-2&view_days=10

The output is an endless loop with starts with this:

Start Day = -2, View_Days= 10
Mon 26 Mar
Sun 25 Mar
Sat 24 Mar
Fri 23 Mar
Thu 22 Mar
Wed 21 Mar
Tue 20 Mar
Mon 19 Mar
Wed 28 Mar
Tue 27 Mar
Mon 26 Mar
Sun 25 Mar
Sat 24 Mar
Fri 23 Mar
Thu 22 Mar
Wed 21 Mar
Tue 20 Mar
Mon 19 Mar
Wed 28 Mar
Tue 27 Mar
Mon 26 Mar
Sun 25 Mar
Sat 24 Mar
Fri 23 Mar
Thu 22 Mar
Wed 21 Mar

Like I said, i feel really stupid now. What am I overlooking ?

Much regards, keep up the good work,

Ace Suares


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-29 06:55 UTC] stas@php.net
Your mistake is that you regard $start_day as number. In
most cases it's OK, but ++ acts differently on strings. So
use $itchy = $start_day+0 or any other mean to make $itchy
explicitly numeric.   
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 04:01:28 2024 UTC