470,848 Members | 1,587 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

why does this if () always evaluate to true?

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>";
}
Jul 17 '05 #1
8 2080
"NotGiven" <no****@nonegiven.net> wrote in message
news:Vw*******************@bignews3.bellsouth.net. ..
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>";
}


I think the function you're looking for is is_numeric()
(http://us2.php.net/manual/en/function.is-numeric.php), not abs().

- JP
Jul 17 '05 #2
thanks but acutally they are measuring different things. Mine evaluates
whether the absolute value is == to the value itself. is_numeric determines
whether it's numeric - could still be negative.

"kingofkolt" <je**********@comcast.net> wrote in message
news:CR5%c.254581$8_6.131379@attbi_s04...
"NotGiven" <no****@nonegiven.net> wrote in message
news:Vw*******************@bignews3.bellsouth.net. ..
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>";
}


I think the function you're looking for is is_numeric()
(http://us2.php.net/manual/en/function.is-numeric.php), not abs().

- JP

Jul 17 '05 #3
.oO(NotGiven)
I have the code below that always evaluates to true. Why and what do I do
about it? Many thanks in advance!
It's because of PHP's implicit type casting and interpreting of numbers
stored in strings.
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.
Your code will evaluate to FALSE only for negative numbers. For your
examples the results after an implicit type cast are:

23 => 23
23.2 => 23.2
23sadkjsi8 => 23
aanns => 0

Obviously these numbers are all positive. PHP also casts the right hand
side of the comparison statement, so

abs(x) == x

=> TRUE
if (abs($_GET['identNum']) == $_GET['identNum']) {
echo "<H1>GOOD NUMBER</H1>";
} else {
echo "<H1>NOT A GOOD NUMBER</H1>";
}


Some questions:

1) Why do you need such things at all?
2) How do you define a "good number", is it any valid int or float
number?

I wouldn't bother about good or bad numbers, but simply take what PHP is
able to make from it (using floatval() for example) and then check if
the submitted number is within a valid range if necessary.

HTH
Micha
Jul 17 '05 #4
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
Jul 17 '05 #5
Thanks - that was almost there. I changed it to integer,

if (is_integer($_GET['identNum']) && $_GET['identNum'] >= 0)

And that worked the way I needed.

Thanks so much for everyone's help!
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:qq********************************@4ax.com...
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

Jul 17 '05 #6
NotGiven wrote:
In the code, I have tried sending in the URL
?indentNum=23
?indentNum=23.2
?indentNum=23sadkjsi8
?indentNum=aanns


Thanks - that was almost there. I changed it to integer,

if (is_integer($_GET['identNum']) && $_GET['identNum'] >= 0)

And that worked the way I needed.

Thanks so much for everyone's help!


You have a spelling mistake in there too - if you were passing
indentNum=23 then shouldn't you be checking $_GET['indentNum'] rather
than $_GET['identNum'] (missing 'n' in 'indent').

--
Jasper Bryant-Greene
Cabbage Promotions
Jul 17 '05 #7
thanks

"Jasper Bryant-Greene" <ja******@gmail.com> wrote in message
news:Gh*********************@news.xtra.co.nz...
NotGiven wrote:
In the code, I have tried sending in the URL
?indentNum=23
?indentNum=23.2
?indentNum=23sadkjsi8
?indentNum=aanns


Thanks - that was almost there. I changed it to integer,

if (is_integer($_GET['identNum']) && $_GET['identNum'] >= 0)

And that worked the way I needed.

Thanks so much for everyone's help!


You have a spelling mistake in there too - if you were passing
indentNum=23 then shouldn't you be checking $_GET['indentNum'] rather than
$_GET['identNum'] (missing 'n' in 'indent').

--
Jasper Bryant-Greene
Cabbage Promotions

Jul 17 '05 #8
.oO(NotGiven)
Thanks - that was almost there. I changed it to integer,

if (is_integer($_GET['identNum']) && $_GET['identNum'] >= 0)

And that worked the way I needed.


Sure?

From <http://www.php.net/manual/en/function.is-int.php>:

"Note: To test if a variable is a number or a numeric string (such as
form input, which is always a string), you must use is_numeric()."

Additionally the above code will cause a notice if there's no
URL-parameter 'indentNum'.

Micha
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rene van Hoek | last post: by
19 posts views Thread by Siemel Naran | last post: by
4 posts views Thread by shumaker | last post: by
9 posts views Thread by David Thielen | last post: by
5 posts views Thread by RobKinney1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.