PHP - Boolean
In PHP, a Boolean is a simple data type that can hold one of two possible values: TRUE
or
FALSE
. Boolean values are often used in control structures such as conditional statements and
loops to determine the flow of execution based on certain conditions.
Defining Booleans
Booleans can be defined directly using the keywords true
and false
(case-insensitive).
Example:
php<?php
$isTrue = true;
$isFalse = false;
?>
Evaluating Boolean Values
PHP automatically converts various types of values to Boolean in conditional statements. The following values
are considered FALSE
in a Boolean context:
- The keyword
false
- The integer
0
- The float
0.0
- An empty string
""
and the string"0"
- An array with zero elements
- An object with zero member variables (PHP 7.2 and earlier)
- The special type
NULL
- A variable declared, but without a value (uninitialized)
All other values are considered TRUE
.
Using Booleans in Conditional Statements
Booleans are typically used in conditional statements like if
, else
,
elseif
, and loops like while
and for
.
Example:
php<?php
$isTrue = true;
if ($isTrue) {
echo "This is true.";
} else {
echo "This is false.";
}
?>
Boolean Operators
PHP supports several logical operators for Boolean values:
1. Logical AND (&&
or and
)
Returns true
if both operands are true.
Example:
php<?php
$a = true;
$b = false;
$result = $a && $b; // false
$result = $a and $b; // false
?>
2. Logical OR (||
or or
)
Returns true
if at least one of the operands is true.
Example:
php<?php
$a = true;
$b = false;
$result = $a || $b; // true
$result = $a or $b; // true
?>
3. Logical NOT (!
)
Negates the Boolean value.
Example:
php<?php
$a = true;
$result = !$a; // false
?>
4. Logical XOR (xor
)
Returns true
if exactly one of the operands is true, but not both.
Example:
php<?php
$a = true;
$b = false;
$result = $a xor $b; // true
?>
Casting to Boolean
You can explicitly cast a variable to Boolean using (bool)
or (boolean)
.
Example:
php<?php
$var = 1; // integer
$boolVar = (bool)$var; // true
?>
Boolean Conversion
PHP converts various types to Boolean based on the rules mentioned earlier.
Example:
php<?php
$var1 = ""; // false
$var2 = "0"; // false
$var3 = 0; // false
$var4 = []; // false
$var5 = null; // false
$var6 = "Hello"; // true
$var7 = 42; // true
echo (bool)$var1; // Output: false
echo (bool)$var2; // Output: false
echo (bool)$var3; // Output: false
echo (bool)$var4; // Output: false
echo (bool)$var5; // Output: false
echo (bool)$var6; // Output: true
echo (bool)$var7; // Output: true
?>
Checking Boolean Values
PHP provides the is_bool()
function to check if a variable is a Boolean.
Example:
php<?php
$var = true;
if (is_bool($var)) {
echo "This is a Boolean.";
} else {
echo "This is not a Boolean.";
}
?>
Summary:
Booleans in PHP are straightforward but essential for controlling the flow of a program. They are used extensively in conditional statements and loops to determine the truthiness of expressions. Understanding how PHP evaluates and manipulates Boolean values is fundamental for writing effective and efficient code.