Page 1 of 2
Is 0 false and 1 true always?
Posted: Sat Feb 03, 2007 10:09 pm
by SmokyBarnable
If I assign a $variable to '0' could I assume that the variable is false and !$variable is true?
Posted: Sat Feb 03, 2007 10:13 pm
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
}
Posted: Sat Feb 03, 2007 10:18 pm
by feyd
If passed through
- empty()... maybe

- is_bool(), no.
- etc, etc...
Posted: Sat Feb 03, 2007 10:36 pm
by SmokyBarnable
I wonder why it works then.
Posted: Sun Feb 04, 2007 4:35 am
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
Posted: Sun Feb 04, 2007 12:47 pm
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.
Posted: Sun Feb 04, 2007 2:36 pm
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
Posted: Sun Feb 04, 2007 3:29 pm
by SmokyBarnable
why does an IF statement need a comparison?
Posted: Sun Feb 04, 2007 3:49 pm
by bokehman
It doesn't.
Seems a bit pointless though, dont you think?
Posted: Sun Feb 04, 2007 4:22 pm
by SmokyBarnable
yes it would be pointless if you assume a variable is always true and will never change.
Posted: Sun Feb 04, 2007 4:28 pm
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?
Posted: Sun Feb 04, 2007 4:46 pm
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;
}
Posted: Sun Feb 04, 2007 4:54 pm
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:
Posted: Sun Feb 04, 2007 5:22 pm
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.
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.
Posted: Sun Feb 04, 2007 5:41 pm
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.