Is 0 false and 1 true always?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

That is only the case when it is a equality comparison to boolean, but there are many types of equality comparison in PHP.
Then how do you explain this?

Code: Select all

<?php 
$result = var_dump((bool) 1); 
echo $result; 
$result = var_dump((bool) 0); 
echo $result; 
?>
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

SmokyBarnable wrote:Then how do you explain this?
Are you on a wind-up? Of 0 will equal false if you convert it to a boolean first but I don't see the connection between that and my comment that there are many types of equality comparison in PHP.
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

I don't see the connection between that and my comment that there are many types of equality comparison in PHP.
I don't understand where the equality comparison is in this statment.

Code: Select all

if  ($variable) {
}
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Unbalanced conditionals do an equality comparison to boolean.
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

I see. So even if zero equates to false I shouldn't assume that it always will?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

It only evalutes to false when checked as a boolean. If you start checking against other types you will get different results.
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

got it.

thanks for your help.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Post Reply