On Mon, 6 Sep 2004 18:31:05 -0400, "NotGiven" <no****@nonegiven.net> wrote:
I have the code below that always evaluates to true. Why and what do I do
about it? Many thanks in advance!
In the code, I have tried sending in the URL
?indentNum=23
?indentNum=23.2
?indentNum=23sadkjsi8
?indentNum=aanns
No matter what I throw at it, it always evaluates to true despite echo'ing
different numbers.
=============================================== ==
echo "<H1>abs value = ".abs($_GET['indentNum'])."</H1>";
echo "<H1>GET = ".$_GET['indentNum']."</H1>";
if (abs($_GET['identNum']) == $_GET['identNum']) {
echo "<H1>GOOD NUMBER</H1>";
} else {
echo "<H1>NOT A GOOD NUMBER</H1>";
}
Well, it's true for all those cases:
abs(23) == 23
abs(23.2) == 23.2
abs('23sadkjsi8') == 23
but since it's comparing them as numbers '23sadkjsi8' == 23 as well
abs('aanns') == 0
but since it's comparing them as numbers, 'aanns' == 0 as well
<pre>
<?php
$a = array('23', '23.2', '23sadkjsi8', 'aanns');
foreach ($a as $i)
{
var_dump($i);
var_dump(abs($i));
var_dump((float)$i);
var_dump((int)$i);
var_dump(abs($i) == $i);
print "--\n";
}
?>
</pre>
Output:
string(2) "23"
int(23)
float(23)
int(23)
bool(true)
--
string(4) "23.2"
float(23.2)
float(23.2)
int(23)
bool(true)
--
string(10) "23sadkjsi8"
int(23)
float(23)
int(23)
bool(true)
--
string(5) "aanns"
int(0)
float(0)
int(0)
bool(true)
--
Do you want "if (is_numeric($_GET['identNum']) && $_GET['identNum'] >= 0)" ?
--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool