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
SmokyBarnable
Forum Contributor
Posts: 105 Joined: Wed Nov 01, 2006 5:44 pm
Post
by SmokyBarnable » Sat Feb 03, 2007 10:09 pm
If I assign a $variable to '0' could I assume that the variable is false and !$variable is true?
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 10:13 pm
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
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Feb 03, 2007 10:18 pm
If passed through
empty()... maybe
is_bool(), no.
etc, etc...
SmokyBarnable
Forum Contributor
Posts: 105 Joined: Wed Nov 01, 2006 5:44 pm
Post
by SmokyBarnable » Sun Feb 04, 2007 12:47 pm
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.
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Sun Feb 04, 2007 2:36 pm
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
SmokyBarnable
Forum Contributor
Posts: 105 Joined: Wed Nov 01, 2006 5:44 pm
Post
by SmokyBarnable » Sun Feb 04, 2007 3:29 pm
why does an IF statement need a comparison?
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Sun Feb 04, 2007 3:49 pm
It doesn't.
Seems a bit pointless though, dont you think?
SmokyBarnable
Forum Contributor
Posts: 105 Joined: Wed Nov 01, 2006 5:44 pm
Post
by SmokyBarnable » Sun Feb 04, 2007 4:22 pm
yes it would be pointless if you assume a variable is always true and will never change.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Feb 04, 2007 4:28 pm
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?
SmokyBarnable
Forum Contributor
Posts: 105 Joined: Wed Nov 01, 2006 5:44 pm
Post
by SmokyBarnable » Sun Feb 04, 2007 4:46 pm
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;
}
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Sun Feb 04, 2007 4:54 pm
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:
SmokyBarnable
Forum Contributor
Posts: 105 Joined: Wed Nov 01, 2006 5:44 pm
Post
by SmokyBarnable » Sun Feb 04, 2007 5:22 pm
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.
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Sun Feb 04, 2007 5:41 pm
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.