Variable multiplication not working as expected...

Hi guys, it’s been a while since I taught myself PHP, and I’m going back and noticing that I can’t seem to get these variables to multiply like I want them.

Here’s the code I’m working on:

[code]<?php

$daylength = 86400; //606024

$current_date = time();

$days_ago = 0;
$secs_ago = $days_ago * $daylength;
$then_date = ($current_date - $secs_ago);
$filedate = date(‘Y-m-d’, $then_date);
$filename = “file_$filedate.html”;

// Testing variables
print "
$daylength = $daylength

$current_date = $current_date

$days_ago = $days_ago

$secs_ago = $secs_ago

$then_date = $then_date

$filedate = $filedate

$filename = $filename


";

print "

Here's a list of the daily files.

Today:


";

if(file_exists($filename)) {
print("$filedate
");
}

print "


Previous:


";

for($days_ago = 1; $days_ago += 1; file_exists($filename)) {

// Testing variables
print "
$daylength = $daylength

$current_date = $current_date

$days_ago = $days_ago

$secs_ago = $secs_ago

$then_date = $then_date

$filedate = $filedate

$filename = $filename

";

    print("<a href='./$filename'>$filedate</a><br /><br />");

}

print "


";

?>[/code]

So the idea is that the loop increases $days_ago by 1 each time, and thus it is supposed to roll backwards printing a link to each previous file until there are no more previous files. But as proven by the variable tests, when $days_ago is incremented and I subsequently call $secs_ago (via $then_date via $filedate via $filename), $secs_ago always ends up as 0, thus making the loop infinitely print today’s file link.

So can someone tell me why $secs_ago, when called, does not reflect its own calculation of $daylength * $days_ago? You can see what I mean in the current output below:

[code]$daylength = 86400
$current_date = 1204030895
$days_ago = 0
$secs_ago = 0
$then_date = 1204030895
$filedate = 2008-02-26
$filename = file_2008-02-26.html

Here’s a list of the daily files.

Today:

2008-02-26

Previous:

$daylength = 86400
$current_date = 1204030895
$days_ago = 2
$secs_ago = 0
$then_date = 1204030895
$filedate = 2008-02-26
$filename = file_2008-02-26.html
2008-02-26

$daylength = 86400
$current_date = 1204030895
$days_ago = 3
$secs_ago = 0
$then_date = 1204030895
$filedate = 2008-02-26
$filename = file_2008-02-26.html
2008-02-26

$daylength = 86400
$current_date = 1204030895
$days_ago = 4
$secs_ago = 0
$then_date = 1204030895
$filedate = 2008-02-26
$filename = file_2008-02-26.html
2008-02-26



…[/code]

Thanks in advance,
== Matt

I don’t like the way this looks:

for($days_ago = 1; $days_ago += 1; file_exists($filename)) {

The correct syntax is:

[code]for(start_statement; end_condition; incremental_statement) {

//example:
for ($i = 0; $i < $some_defined_number; $i++) {[/code]

I realized also that I have to update the variables ever time I change one. But yeah, I can’t believe I forgot the for loop syntax! Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service