472,989 Members | 2,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

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 2164
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rene van Hoek | last post by:
Hi, I am using Xalan 1.8.0 and Xerces 2.6.0 in C++. I have an XML document which I first transform into an other XML document using an XSL styelsheet. Then I want to parse with XPathEvaluator...
19
by: Siemel Naran | last post by:
Does true ^ true return false? int silly() { return 3; } int main() { bool t1 = silly(); bool t2 = true; cout << (t1 ^ t2) << '\n'; }
4
by: shumaker | last post by:
I'm wondering how/why this query works. Trying to get my head wrapped around SQL. Basically the Query deletes from the Import table all records that are already in FooStrings so that when I do an...
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
6
by: moosdau | last post by:
it's likely that the compiler could only promise returning a non-zero value in my impression, but it did return 1 every time, like the code below: int a=3,b=8; printf("%d\n",a<b); will it ...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
5
by: RobKinney1 | last post by:
Wow... unbelieveable that this problem would arise right before giving the software to our public testers... or maybe it is believable. We tweaked some seemingly unrelated code somewhere else...
7
by: bonk | last post by:
I have a c# project as part of a larger VS 2005 solution that always gets build optimized and I therefore can not evaluate any values while debugging through the code ("Cannot evaluate expression...
2
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string that looks like this "True AND True AND True" and...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.