Comparing Two Numbers Fails
Steve Matzura <[email protected]> Mon, 7 Oct 2024 21:01:22 -0400
| Newsgroups | gmane.comp.php.general |
|---|---|
| Message-ID | <[email protected]> |
In a script, I am testing two values--$cur_time and $updated_time, and
if their difference is less than or equal to 86400, some processing is
supposed to occur. Just before the test, I have a print statement which
displays the values needed for the if test to succeed, followed by the
if test, followed by what to do if the test succeeds, starting with
another print statement, which is supposed to show me on-screen that the
test succeeds and I'm now into the processing code.
*** Snip ***
$x = ($cur_time - $updated_time); // Don't worry, those variables
really do exist.
print "$showcode, $cur_time, $updated_time, $x\n"; // Display the
variables in question before the test. This always works.
// This shows me what file is being processed, the current time,
// the time it was last updated, and the difference between the two.
// If $x is less than or equal to 86400, the test passes and processing
continues.
// But even when the values all align and are good, it fails.
if (($cur_time - $updated_time) <= 86400) { // 24 hours
print Chosen: "$showcode, $cur_time, $updated_time\n"; // Show me
what file should be on the report.
// This doesn't work, and the rest of the processing never
happens, so the test must have failed. But why?
}
Everything looks correct. It's a stupidly simple if statement. What
could possibly be wrong with it?
I paired it down to the bare essentials and preloaded a couple variables
for test purposes:
<?php
$showcode = "TEST";
$cur_time = time ();
$updated_time = ($cur_time - 50000); // Make like the test should pass.
$x = ($cur_time - $updated_time); // DO the math, mostly for short print
statement.
print "$showcode, $cur_time, $updated_time, $x\n";
// Everything's preloaded and correct.
if ($cur_time - $updated_time <= 86400) //which it is ...
print "Yes! $showcode, $x\n";
?>
When I run this, it works. What could possibly be wrong with the same
decision statements in the first sample? The second sample is just a
single print statement, not a block enclosed by braces, but so what? I
should get the print statement that comes right after the "if" statement
is the computed value of ($cur_time - $updated_time) is less than 86400,
and as the foreach loop goes around, the first print statement shows me
that there are indeed some files in the directory that meet this
criterion. What's going on here? This is keep-it-simple-stupid simple.