RE: [PHP] Please explain...
| From: | Rudolf Visagie | Date: | Tue, 19 Mar 2002 11:31:31 +0000 |
| Subject: | RE: [PHP] Please explain... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-89103@lists.php.net to get a copy of this message | ||
Agreed. I thought that the type of the variable on the left of the
comparison had something to do with it, but then I ran this code:
$x = 0;
if ("foo" == $x) {
echo "This is an integer comparison with foo being ".$x." and on the
left of the comparison<br>";
}
$y = true;
if ("foo" == $y) {
echo "This is a boolean comparison with foo being ".$y." and on the
left of the comparison<br>";
}
and the output is:
This is an integer comparison with foo being 0 and on the left of the
comparison
This is a boolean comparison with foo being 1 and on the left of the
comparison
So all that I can think is that there is a type precedence in == comparisons
with numbers and boolean variables taking precedence over strings. Any
ideas?
-----Original Message-----
From: Andrey Hristov [mailto:ahristov@icygen.com]
Sent: Tuesday, March 19, 2002 1:37 PM
To: php-general@lists.php.net
Subject: Re: [PHP] Please explain...
AFAIK the problem reported comes from that $x is on the left but "foo" is on
the right.
so you convert "foo" to int, and because there is not digit in "foo" it is
0.
<?php
$a="4some";
$b=$a+2;
var_dump($b);
?>
int(6)
<?php
$x=0;
if ($x=="foo") echo "foobar";
?>
<?php
$x=1;
if ("foo"==$x) echo "fubar"; // same with ===
?>
Output:
foobar
"foo" evaluates as 0 in the comparison.
Andrey
----- Original Message -----
From: "Rudolf Visagie" <Rudolf@qedi.co.za>
To: "Alexander Skwar" <ASkwar@host.homelinux.org>
Cc: <php-general@lists.php.net>
Sent: Tuesday, March 19, 2002 1:01 PM
Subject: RE: [PHP] Please explain...
For interest's sake the following code:
$x = 0;
if ($x == "foo") {
echo "This is an integer comparison with foo being ".$x."<br>";
}
$y = true;
if ($y == "foo") {
echo "This is a boolean comparison with foo being ".$y."<br>";
}
prints:
This is an integer comparison with foo being 0
This is a boolean comparison with foo being 1
Cheers
-----Original Message-----
From: Alexander Skwar [mailto:ASkwar@host.homelinux.org]
Sent: Tuesday, March 19, 2002 12:51 PM
To: Rudolf Visagie
Cc: Jan Rademaker; php-general@lists.php.net
Subject: Re: [PHP] Please explain...
»Rudolf Visagie« sagte am 2002-03-19 um 12:19:04 +0200 :
> I would guess that the string "foo" evaluates to integer 0 in the
comparison
Hmm, shouldn't a filled string ("foo") evaluate to TRUE, ie. to 1?
Alexander Skwar
--
How to quote: http://learn.to/quote (german) http://quote.6x.to (english)
Homepage: http://www.iso-top.de | Jabber:
askwar@charente.de
iso-top.de - Die günstige Art an Linux Distributionen zu kommen
Uptime: 1 day 23 hours 44 minutes
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php