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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.