Jump to content

Why is this evalutaing to TRUE?


doubledee

Recommended Posts

I knew that was so in an IF condition, but I did not know that if you put any non-zero value in a logical comparison that it would evaluate to TRUE.

 

It has to do with how PHP decides to interpret the types for the comparison.  Since your testing a boolean==integer, php decides to cast the integer to a boolean and evaluate the condition as boolean==boolean.

 

I thought the exact OPPOSITE was happening...

 

I thought PHP turned TRUE into "1" and so 1 would never equal 999.

 

Interesting...

 

 

so:

$preview == 999 becomes TRUE == (boolean)999 which becomes TRUE == TRUE.

 

Hey, that's cool.  Thanks for the explanation!  8)

 

 

You can force php to interpret something a specific way by doing your own casting, such as:

if ((int)$preview == 999):

 

which would force php to cast $preview to an int (1) then evaluate 1 == 999, which would be false.

 

And that is how I thought things were working.

 

(PHP does things a lot differently than languages like Java, C, and C++)

 

Thanks for the extra information!!

 

 

Debbie

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.