RE: Strange 'if' test bug?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The only thing I can think of, and it still doesn't totally add up, but
when you do "$key = 0;", you're assigning an integer to $key.  You then
do a comparison of 0 (integer) == "muppet" (string).

0 is also symbolic of "false" as well, correct?

If I replace 0 with FALSE, I get "key is something else: " (nothing
after the ":") but when I replace 0 with TRUE, I get "key is a muppet:
1".

Anyway, I was wondering if it had something to do with variable typing.
I've heard PHP referred to as "typeless" but variables do have a type
and if you're not explicit about what you want it to be, it'll auto-type
the variable based on what data you assign to it.

$key = 0;   # $key is type int
$key = "zero"; # $key is now type string

If you do:
$key = (string) 0;

It's the same as doing:
$key = "0";

Btw, Here's the passage from the PHP help file about typing:
"PHP does not require (or support) explicit type definition in variable
declaration; a variable's type is determined by the context in which
that variable is used. That is to say, if you assign a string value to
variable $var, $var becomes a string. If you then assign an integer
value to $var, it becomes an integer."

The reason my brain went down this path is because "===" means something
is "identical".  Same value AND type:

"$a == $b Equal TRUE if $a is equal to $b. 
$a === $b Identical TRUE if $a is equal to $b, and they are of the same
type. (PHP 4 only)"

(again from the PHP help)

So it sounds like it's type related, but it still doesn't make any
sense.  "muppet" should evaluate to TRUE according to that PHP cheat
sheet that was posted.  And 0 should evaluate to FALSE.

Interesting issue.  Thanks for posting about it Steve!

Maybe it's some configuration difference between your linux system and
your windows box.  Either system or some PHP.INI type thing. Let us know
if you figure it out.

-TG

> -----Original Message-----
> From: Steve McGill [mailto:steve@xxxxxxxxxxxxx] 
> Sent: Tuesday, August 31, 2004 4:35 AM
> To: php-windows@xxxxxxxxxxxxx
> Subject:  Strange 'if' test bug?
> 
> 
> Sorry if this is a newbie FAQ,
> 
> <?php
> $key=0;
> if($key == "muppet") {
>   echo "key is a muppet: $key";
> } else {
>   echo "key is something else: $key";
> }
> ?>
> 
> Will print completely the wrong line:
> 
> ====> key is a muppet: 0
> 
> If I use three equals signs, i.e. ===, then the test works.
> 
> But if I use linux, the test works as expected with two equals signs.
> 
> What on earth am I missing?
> 
> Using Windows PHP 4.3.8 as a CGI, on Apache 1.3.31.
> 
> Many thanks in advance for any help,
> Steve
> 
> -- 
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux