A Boolean in PHP represents true or false values.
It is commonly used in conditions, loops, and logical operations.
This tutorial will cover:
Table of Contents
1. What Are Boolean Values in PHP?
A Boolean is a data type that can have only two values:
- true
- false
Example:
<?php $bool1 = true; $bool2 = false; var_dump($bool1); // Output: bool(true) var_dump($bool2); // Output: bool(false) ?>
- The var_dump() function is used to check the data type.
2. Declaring and Using Boolean Variables
Example:
<?php
$is_active = true;
$is_logged_in = false;
if ($is_active) {
echo "The user is active!";
} else {
echo "The user is not active.";
}
?>
- if ($is_active) is true, so it prints “The user is active!”.
3. Boolean Comparison Operators
Comparison operators return Boolean values (true or false).
Comparison Operators in PHP
| Operator | Description | Example |
|---|---|---|
| == | Equal to | $a == $b |
| != | Not equal to | $a != $b |
| === | Identical (equal and same type) | $a === $b |
| !== | Not identical | $a !== $b |
| > | Greater than | $a > $b |
| < | Less than | $a < $b |
| >= | Greater than or equal | $a >= $b |
| <= | Less than or equal | $a <= $b |
Example:
<?php $x = 10; $y = 20; var_dump($x > $y); // Output: bool(false) var_dump($x < $y); // Output: bool(true) var_dump($x == 10); // Output: bool(true) ?>
4. Boolean Logical Operators
Logical operators are used to combine Boolean expressions.
Logical Operators in PHP
| Operator | Description | Example |
|---|---|---|
| && | AND (Both conditions must be true) | $a && $b |
| ` | ` | |
| ! | NOT (Reverses the condition) | !$a |
Example:
<?php
$is_admin = true;
$is_logged_in = false;
// AND Operator
if ($is_admin && $is_logged_in) {
echo "Admin panel access granted!";
} else {
echo "Access denied!";
}
// OR Operator
if ($is_admin || $is_logged_in) {
echo "Limited access granted!";
}
?>
- The && (AND) condition fails because one value is false.
- The || (OR) condition passes because one value is true.
5. Boolean Type Casting and Conversion
PHP automatically converts other data types to Boolean when used in a Boolean context.
Truthy and Falsy Values in PHP
| Value | Boolean Conversion |
|---|---|
| 0 | false |
| “” (empty string) | false |
| “0” (string “0”) | false |
| null | false |
| [] (empty array) | false |
| 42, -42 (any non-zero number) | true |
| “hello” (any non-empty string) | true |
| [1,2,3] (non-empty array) | true |
Example:
<?php var_dump((bool) 0); // Output: bool(false) var_dump((bool) 42); // Output: bool(true) var_dump((bool) "hello"); // Output: bool(true) var_dump((bool) ""); // Output: bool(false) var_dump((bool) []); // Output: bool(false) ?>
- (bool) $variable converts the value to a Boolean.
6. Boolean in Conditional Statements
Booleans are commonly used in if-else conditions.
Example:
<?php
$is_logged_in = true;
if ($is_logged_in) {
echo "Welcome, user!";
} else {
echo "Please log in.";
}
?>
- if ($is_logged_in) is true, so “Welcome, user!” is printed.
7. Boolean in Loops
Booleans can control loops.
Example: Using Boolean in a While Loop
<?php
$counter = 0;
$keep_running = true;
while ($keep_running) {
echo "Iteration: $counter <br>";
$counter++;
if ($counter >= 5) {
$keep_running = false; // Stop the loop
}
}
?>
- The loop runs until $keep_running is false.
8. Boolean Functions in PHP
Some PHP functions return Boolean values.
Example Functions:
| Function | Description |
|---|---|
| is_bool($var) | Checks if a variable is Boolean |
| empty($var) | Returns true if a variable is empty |
| isset($var) | Returns true if a variable is set |
| filter_var($var, FILTER_VALIDATE_BOOLEAN) | Converts a value to Boolean |
Example:
<?php $value = true; var_dump(is_bool($value)); // Output: bool(true) $name = ""; var_dump(empty($name)); // Output: bool(true) $option = "yes"; var_dump(filter_var($option, FILTER_VALIDATE_BOOLEAN)); // Output: bool(true) ?>
Conclusion
- Booleans (true/false) are fundamental in PHP for logical operations and flow control.
- Comparison and logical operators help in decision-making.
- PHP automatically converts values to Boolean in conditions.
- Boolean functions assist in checking and validating data.