Page 1 of 1
$var = FALSE or 'FALSE'
Posted: Mon Nov 14, 2005 1:55 pm
by seodevhead
Is...
.. the same as:
I want to use $var in a conditional to test if true or false, like:
Code: Select all
if ($var)
{ echo 'TRUE!'; }
else
{ echo 'FALSE!'; }
Thanks!
Posted: Mon Nov 14, 2005 1:58 pm
by hawleyjr
False in strings such as 'FALSE' is the string FALSE False w/o the quotes is False as in No or 0 (Zero) Boolean.
Posted: Mon Nov 14, 2005 2:00 pm
by hawleyjr
Oops, the code you need.
Code: Select all
$var = FALSE;
if ($var === TRUE)
{ echo 'TRUE!'; }
else
{ echo 'FALSE!'; }
Posted: Mon Nov 14, 2005 2:04 pm
by seodevhead
thanks for the heads up...
real quick...
why === instead of just the standard ==???
Is === for boolean comparison?'
Thanks.
Posted: Mon Nov 14, 2005 2:07 pm
by jwalsh
=== means an exact match of value and variable type.
so 12 as a string will fail against 12 as an integer.
Posted: Mon Nov 14, 2005 2:07 pm
by Nathaniel
=== is for type comparison. true === true will pass, true === 'true' will not (because while they are both "true", the one is a boolean and the other is a string).
Posted: Mon Nov 14, 2005 2:08 pm
by hawleyjr
When doing boolean searches. Its best to use === (3 equal signs) This makes it an exact comparison.
Code: Select all
$myVar = 'abcdefg';
if($myVar){
//would be true
}
if($myVar == TRUE){
//would be true
}
if($myVar === TRUE){
//would not be true;
}
EDIT: Not Search Comparison
Posted: Mon Nov 14, 2005 2:20 pm
by trukfixer
yes . = means assignment
== means compare for equality
=== means compare for equality *AND* type
So..
Code: Select all
//given the following
$fred = 5;
$wilma = "true"'
$barney = true;
if($fred == $wilma)
{
echo "fred matches wilma<br>";
}
elseif($fred == $barney)
{
echo "fred matches barney<br>";
}
else
{
echo "No match<br>";
}
if($wilma == $barney)
{
echo "wilma matches barney<br>";
}
elseif($wilma == $fred)
{
echo "Wilma matches fred<br>";
}
else
{
echo "Wilma doesnt match anybody<br>";
}
if($fred === $barney)
{
echo "fred is like barney<br>";
}
elseif($wilma === $barney)
{
echo "Wilma matches Barney<br>";
}
else
{
echo "No matches found<br>";
}
//OK.. see if you can figure out *WHICH* values will be echoed by reading the code before peeking at the answer
Interesting isnt it? easy to get confused
Well I can tell you , the results will be like this:
fred matches barney
Wilma doesnt match anybody
No matches found
note the == comparision will return true when matching fred and barney, even though fred = 5 and barney = 1 (boolean true)
Wilma, even though is "true" (string type), doesnt match barney..
and lastly, there are no matches found when you compare the previous "fred == barney" as "fred === barney" because the third = checks value *AND* type..
So..
$testone = (int)1;
$testtwo = (bool)1;
$testone == $testtwo returns true, while $testone === $testtwo returns false. interesting, eh?
**EDIT** Heh.. OK.. so I type out a long winded explanation and 3 other posts sneak in ahead of me.. Oh well. that tells me I should just shut up

** / EDIT **
Posted: Mon Nov 14, 2005 2:49 pm
by Ambush Commander
There is also type juggling at work here.
Any string that is not empty or containing 0, such as:
Code: Select all
$var = (bool) '';
$var = (bool) '0';
Will return true. Therefore...
Code: Select all
$var = (bool) 'false';
$var = (bool) 'zero';
$var = (bool) 'off';
$var = (bool) ' ';
These all cast to true. Therefore...
Code: Select all
if('false') {
//this will always be executed
}