By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

Arithmetic error involving 0

P: n/a
I get the output:

age=0;

age=0;

from the following code:

$debug .= "<br>age=$age;<br>";
if($age!="unk") $age = $age+1;
$debug .= "<br>age=$age;<br>";

What is going on?
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
why do you have semicolons after $age inside of the quotations?

remove those, and see if you still get the error.

Jul 17 '05 #2

P: n/a
Bob Stearns wrote:
I get the output:

age=0;

age=0;

from the following code:

$debug .= "<br>age=$age;<br>";
if($age!="unk") $age = $age+1;
$debug .= "<br>age=$age;<br>";

What is going on?


IIRC there are some odd issues in PHP when comparing 0 with a string. If
you change it to do a strict type check with !== it solves the
immediate issue:

if($age!=="unk") $age = $age+1;

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #3

P: n/a
dspohn wrote:
why do you have semicolons after $age inside of the quotations?

remove those, and see if you still get the error.


And why do you think that would make a difference?

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #4

P: n/a
Chris Hope wrote:
Bob Stearns wrote:
I get the output:

age=0;

age=0;

from the following code:

$debug .= "<br>age=$age;<br>";
if($age!="unk") $age = $age+1;
$debug .= "<br>age=$age;<br>";

What is going on?


IIRC there are some odd issues in PHP when comparing 0 with a string.
If you change it to do a strict type check with !== it solves the
immediate issue:

if($age!=="unk") $age = $age+1;


I found the reason.

From: http://www.php.net/manual/en/languag...comparison.php

"If you compare an integer with a string, the string is converted to a
number. If you compare two numerical strings, they are compared as
integers."

var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true

Unless it is at all numerical the string will be converted to 0 making a
comparison with 0 true. In your example the comparison ends up being
0 != 0 which is of course false.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #5

P: n/a
Chris Hope wrote:
Chris Hope wrote:

Bob Stearns wrote:

I get the output:

age=0;

age=0;

from the following code:

$debug .= "<br>age=$age;<br>";
if($age!="unk") $age = $age+1;
$debug .= "<br>age=$age;<br>";

What is going on?


IIRC there are some odd issues in PHP when comparing 0 with a string.
If you change it to do a strict type check with !== it solves the
immediate issue:

if($age!=="unk") $age = $age+1;

I found the reason.

From: http://www.php.net/manual/en/languag...comparison.php

"If you compare an integer with a string, the string is converted to a
number. If you compare two numerical strings, they are compared as
integers."

var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true

Unless it is at all numerical the string will be converted to 0 making a
comparison with 0 true. In your example the comparison ends up being
0 != 0 which is of course false.

I saw that some time ago and forgot it. Thanks for the reminder.
Jul 17 '05 #6

P: n/a
Chris Hope wrote:
Bob Stearns wrote:

I get the output:

age=0;

age=0;

from the following code:

$debug .= "<br>age=$age;<br>";
if($age!="unk") $age = $age+1;
$debug .= "<br>age=$age;<br>";

What is going on?

IIRC there are some odd issues in PHP when comparing 0 with a string. If
you change it to do a strict type check with !== it solves the
immediate issue:

if($age!=="unk") $age = $age+1;

I saw that some time ago and forgot it. Thanks for the reminder.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.