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

Is 0 false and 1 true always?

Post by SmokyBarnable »

If I assign a $variable to '0' could I assume that the variable is false and !$variable is true?

Code: Select all

if (!$variable) {
         do something
          }
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

I don't think so. $variable will set it to '0' means the value of $variable ='0' and is set. Instead you can try setting the variable to false always.

Code: Select all

$variable=false;
if (!$variable) { 
         do something 
          }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If passed through
  • empty()... maybe ;)
  • is_bool(), no.
  • etc, etc...
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

I wonder why it works then.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Your comparison if($x), if (!$x) allows implicit type casting. The rules for casting to boolean are listed at http://www.php.net/manual/en/language.t ... an.casting
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

I think I understand.

So by having a value in an IF statement that is not boolean then php automatically converts the number to True or False.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

SmokyBarnable wrote:So by having a value in an IF statement that is not boolean then php automatically converts the number to True or False.
No. It depends what is being compared to what.

Code: Select all

if('a' == 0) // true
if('a' == false) // false
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

why does an IF statement need a comparison?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

It doesn't.

Code: Select all

if(true)
Seems a bit pointless though, dont you think?
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

yes it would be pointless if you assume a variable is always true and will never change.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The point is/was that if you're going to perform a conditional on a constant, it is pointless because the result is always the same.

For example

Code: Select all

$if = 'a';

if($if)
{
  // whatever
}
else
{
  // some other such
}
$if will not change, so what's the point of having a block of code that won't be processed?
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

because the variable will either be a 1 or a zero, and if my variable equals zero then the if statement executes like this:

Code: Select all

$variable = 0;
//if the variable is zero change it to one
if (!$variable)
 {
  $variable = 1;
 }
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

If your variable will always start as 0 or 1 that piece of code is redundant since the final outcome is that $variable will always end up having the value 1 which would be more efficiently done like this:

Code: Select all

$variable = 1;
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

Code: Select all

$variable = 0; 
//if the variable is zero do something
if (!$variable) 
 { 
  do something
  $variable = 1; 
 }
Is it redundant now?

My original question has be answered by volka.
Your comparison if($x), if (!$x) allows implicit type casting. The rules for casting to boolean are listed at http://www.php.net/manual/en/language.t ... an.casting
To explicitly convert a value to boolean, use either the (bool) or the (boolean) cast. However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.
So to answer my own question. Yes, I can assume $variable = 0 is false because the value will be automatically converted if an operator, function or control structure requires a boolean argument.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

SmokyBarnable wrote:if an operator, function or control structure requires a boolean argument.
That is only the case when it is a equality comparison to boolean, but there are many types of equality comparison in PHP.
Post Reply