$var = FALSE or 'FALSE'

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

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

$var = FALSE or 'FALSE'

Post by seodevhead »

Is...

Code: Select all

$var = FALSE;
.. the same as:

Code: Select all

$var = 'FALSE';


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!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Oops, the code you need.

Code: Select all

$var = FALSE;


if ($var === TRUE)
{ echo 'TRUE!'; }
else
{ echo 'FALSE!'; }
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

thanks for the heads up...

real quick...

why === instead of just the standard ==???

Is === for boolean comparison?'

Thanks.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

=== means an exact match of value and variable type.

so 12 as a string will fail against 12 as an integer.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post 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).
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post 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 **
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
}
Post Reply