How do you check if a boolean is true in php?

I know this question is not really important.. however I've been wondering:

Which of the following IF statements is the best and fastest to use?


I know === is to match exactly the boolean value. However is there really any improvement?

asked Nov 3, 2009 at 21:07

How do you check if a boolean is true in php?

MarioRicaldeMarioRicalde

8,7936 gold badges39 silver badges41 bronze badges

2

Using if ($var === true) or if ($var) is not a question of style but a question of correctness. Because if ($var) is the same as if ($var == true). And == comparison doesn’t check the type. So 1 == true is true but 1 === true is false.

answered Nov 3, 2009 at 21:09

0

As far as speed, I agree with Niels, it's probably negligible.

As far as which if statement is best to test with, the answer probably depends on the expected casting and values $variable can have.

If $variable is using 0 and 1 as a true/false flag then if ( $variable ) or if ( !$variable ) would work, but if it's an integer result as in strpos() you'll run into problems ... if possible, I'd recommend using an actual boolean value rather than 0 / 1.

... maybe this will help clarify; comment out the variations of $var to see the various results.

';
}

if ( $var === true ) {
    echo 'var is a boolean and = true';
}

if ( !$var ) {
    echo 'var = false 
'; } if ( $var === false ) { echo 'var is a boolean and = false'; }

Just a fact:

time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r == true) {} }'

time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r) {} }'

The second one is faster than the first.

gmadd

1,1469 silver badges17 bronze badges

answered Feb 1, 2010 at 18:48

I'm not really deep into the technical stuff of PHP, but in the first case

if($variable === true)

$variable has to have the exact same type as true for the if statement to be true. In other words, $variable has not only to resolve to true, but also has to be a boolean. So that's 2 operations, value-checking and type-checking.

In the second case

if($variable)

$variable only has to resolve to true. So only value checking occurs. I infer that that takes the computer a little less time.

Practically speaking: the differences in speed are probably negligible.

answered Nov 3, 2009 at 21:14

Niels BomNiels Bom

8,2328 gold badges44 silver badges57 bronze badges

=== is really helpful in strstr/stristr when the first needle is in position 0 in the haystack. If you don't use === or !== you could have a bug on your hands.

$str = "four score and...";
$position = strstr($str,'four');
if($position===FALSE) return FALSE;

answered Nov 3, 2009 at 21:12

easementeasement

6,0713 gold badges28 silver badges35 bronze badges

1

The function Michael Smith published doesn't do the work as it should.

To check if a variable is boolean is one thing, to evaluate if the value of a variable represents a boolean condition (true or false) is another.

Here a simple function that checks the status of the received variable in regards to boolean equivalencies (case insensitive).

/**
* Check "Booleanic" Conditions :)
*
* @param  [mixed]  $variable  Can be anything (string, bol, integer, etc.)
* @return [boolean]           Returns TRUE  for "1", "true", "on" and "yes"
*                             Returns FALSE for "0", "false", "off" and "no"
*                             Returns NULL otherwise.
*/
function is_enabled($variable)
{
    if (!isset(
$variable)) return null;
    return
filter_var($variable, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
?>

Of course, it is a simplistic approach, but for the majority of cases it will do the job right.

And, just to put thing in the right perspective, here's a real function that does what Phill disclosed:

/**
* Convert $variable to boolean (adapted from Phill answer)
*
* @param  [mixed]  $variable  Can be anything
* @return [boolean]           Returns the booelan equivalent to $variable based on Zend Enegine interpretation
*/
function to_bool($variable)
{
    return (bool)
$variable;
}
?>

I hope it helps someone. Happy coding.

How do you know if a boolean value is true?

To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean. Copied!

What evaluates to true in PHP?

Summary. A boolean value represents a truth value, which is either true or false . PHP evaluates the following values to false: false, 0, 0.0, empty string (“”), “0”, NULL, an empty array; other values are true .

Is 1 true or false in PHP?

The boolean values are called true and false in php. In the case of true , the output is 1 . While with the false , it does not show any output.

Is bool true 1 or 0?

The boolean type. The bool represents a value, which could only be either true or false . If you cast a bool into an integer, true will be 1 and false will be 0.