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.

In PHP, true is a reserved keyword and a boolean literal representing the affirmative truth state in boolean logic. It is one of the two possible states of the scalar bool data type, the other being false.

Syntax and Case Sensitivity

The true literal is case-insensitive. However, the PHP-FIG PSR-12 coding standard mandates the use of strictly lowercase letters for all reserved keywords, including boolean literals.
$a = true;  // Standard (PSR-12 compliant)
$b = TRUE;  // Valid, but non-standard
$c = True;  // Valid, but non-standard

var_dump($a === $b); // bool(true)

Type Coercion and Casting

When PHP’s Zend Engine coerces or explicitly casts the true literal into other scalar or compound types, it follows strict internal conversion rules:
  • Integer: Casts to 1.
  • Float: Casts to a floating-point 1.0 (Note: var_dump outputs whole-number floats without the .0 decimal).
  • String: Casts to "1" (Note: false casts to an empty string "").
  • Array: Casts to an indexed array containing the boolean as its single element at index 0.
var_dump((int) true);    // int(1)
var_dump((float) true);  // float(1)
var_dump((string) true); // string(1) "1"
var_dump((array) true);  // array(1) { [0]=> bool(true) }

Comparison Mechanics

Because PHP is a dynamically typed language, true behaves differently depending on whether an equality (==) or identity (===) operator is used. Under loose comparison (==), PHP performs type juggling, coercing operands to booleans. Specific “falsy” values—such as 0, 0.0, "0", "", null, and empty arrays []—evaluate to false, while most other values evaluate to true. Under strict comparison (===), no type coercion occurs; the operand must be of type bool and hold the exact value of true.
// Loose comparison (Type juggling occurs)
var_dump(true == 1);       // bool(true)
var_dump(true == "1");     // bool(true)
var_dump(true == "hello"); // bool(true)
var_dump(true == "0");     // bool(false)

// Strict comparison (Type and value must match)
var_dump(true === 1);       // bool(false)
var_dump(true === true);    // bool(true)

Type Verification

To programmatically verify if a variable holds the exact boolean true state, strict comparison (===) is required. Relying solely on type-checking functions like is_bool() is insufficient, as it only verifies the data type and will return true even if the variable holds false.
$value = true;

var_dump(is_bool($value));         // bool(true) - Only confirms type (could be false)
var_dump($value === true);         // bool(true) - Confirms both type and state
Master PHP with Deep Grasping Methodology!Learn More