Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A bool (boolean) is a scalar data type in PHP that represents a logical truth value, strictly capable of holding one of two states: true or false. The boolean constants in PHP are case-insensitive.
$valid = true;
$active = FALSE; // Case-insensitive, though lowercase is the PSR-12 standard

Type Casting and Coercion

PHP is a dynamically typed language and frequently performs implicit boolean coercion during logical operations. Explicit casting is executed using the (bool) or (boolean) cast operators.
$explicitCast = (bool) $variable;

Falsy Values

When a value is implicitly coerced or explicitly cast to a bool, PHP evaluates most values as true (truthy), with a specific set of exceptions that evaluate to false (falsy). The following standard values evaluate to false:
(bool) false;     // The boolean false itself
(bool) 0;         // Integer zero
(bool) 0.0;       // Float zero (and -0.0)
(bool) "";        // Empty string
(bool) "0";       // String containing exactly the character "0"
(bool) [];        // Array with zero elements
(bool) null;      // NULL value (including unset variables)
Note: While almost all other values evaluate to true (including the string "false", the string "0.0", negative integers, and empty stdClass objects), there are exceptions for internal objects that overload their casting behavior. For example, a SimpleXMLElement object created from an empty XML element without attributes evaluates to false.

String Representation

When a bool is cast to a string—such as when passed to an echo or print statement—PHP handles the conversion asymmetrically:
$trueStr = (string) true;   // Evaluates to the string "1"
$falseStr = (string) false; // Evaluates to an empty string ""

echo true;  // Outputs the string "1"
echo false; // Outputs an empty string ""

Type Checking

To verify if a variable is strictly of the bool type without triggering implicit type coercion, use the built-in is_bool() function. To verify if a variable holds a specific boolean value without coercion, use the strict comparison operators (=== / !==).
$var = false;
$intVar = 1;

// Checking type
is_bool($var);     // Returns true (it is of type bool)
is_bool($intVar);  // Returns false (it is an integer)

// Checking strict value
$var === false;    // Returns true (strict type and value check)
$intVar === true;  // Returns false (strict type and value check)

// Loose comparison
$intVar == true;   // Returns true (loose comparison triggers coercion)
Master PHP with Deep Grasping Methodology!Learn More