Boolean Type

Home >>Data Types  >> Boolean


The boolean type returns the value either TRUE or FALSE

Syntax

In order to specify the boolean variable, use the constants TRUE or FALSE. Both are case-insensitive

<?php
$coderz True// assign the value TRUE to $coderz
?>

Typically, the  control structure depends upon the value of boolean operator. Because, it returns either TRUE or FALSE

<?php

// == is an operator which tests
// equality and returns a boolean
if ($name== "coderz") {
    echo "Coderztoday is the best online tutorial";
}

// this is not necessary...
if ($name ==TRUE) {
    echo "<hr>\n";
}

// ...because this can be used with exactly the same meaning:
if ($name ) {
    echo "<hr>\n";
}
?>

Converting to boolean

To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

When converting to boolean, the following values are considered FALSE:
  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource and NAN).

Example
<?php
var_dump((bool) "");        // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)

?>

Comments

Popular posts from this blog

Float

FastCGI Process Manager