Home >Backend Development >PHP Tutorial >Understanding PHP Types – Booleans
In PHP, a Boolean is a data type that can have only two values:
Boolean values are often used in conditional statements, comparisons, and logical operations. It is important to note that both true and false are case-insensitive. So, true, True, truE, TRUE are all valid.
You can create a Boolean variable by assigning the values true or false directly:
Example:
<?php $isAvailable = true; // Boolean variable set to true $isLoggedIn = false; // Boolean variable set to false var_dump($isAvailable); // Output: bool(true) var_dump($isLoggedIn); // Output: bool(false) ?>
The var_dump() function is used here to display the type and value of the variables.
In PHP, many data types can be converted to Boolean. A value can be type casted into boolean using (bool). Generally this is not necessary because when a value is used in a logical context it will be automatically interpreted as a value of type bool.
When converting to boolean, the following values will return false:
All other values are considered true, including resource and NAN.
Example:
<?php $values = [0, 1, "", "Hello", [], [1, 2, 3], null, 0.0, 3.14]; foreach ($values as $value) { var_dump((bool)$value); } ?>
Output:
bool(false) // 0 bool(true) // 1 bool(false) // "" bool(true) // "Hello" bool(false) // [] bool(true) // [1, 2, 3] bool(false) // null bool(false) // 0.0 bool(true) // 3.14
Booleans are primarily used in conditional statements such as if, else, and switch to control the flow of the program.
Example:
<?php $isUserLoggedIn = true; if ($isUserLoggedIn) { echo "Welcome, User!"; // This message will be displayed } else { echo "Please log in."; } ?>
In this example, since $isUserLoggedIn is true, the message “Welcome, User!” will be displayed.
PHP provides various comparison operators that return Boolean values:
Example:
5); // Output: bool(true) - 10 is greater than 5 var_dump($a != 10); // Output: bool(false) - 10 is equal to 10 ?>
Logical Operators and Booleans
Logical operators are used to combine or negate Boolean expressions:
Example:
<?php $isAvailable = true; // Boolean variable set to true $isLoggedIn = false; // Boolean variable set to false var_dump($isAvailable); // Output: bool(true) var_dump($isLoggedIn); // Output: bool(false) ?>
Using == can lead to unexpected results due to type juggling. Always use === for strict comparisons.
Example:
<?php $values = [0, 1, "", "Hello", [], [1, 2, 3], null, 0.0, 3.14]; foreach ($values as $value) { var_dump((bool)$value); } ?>
In the first case, 0 and false are considered equal because 0 converts to false when compared loosely.
Certain values like 0, "0", and empty arrays are considered false, which can sometimes lead to logical errors.
Example:
bool(false) // 0 bool(true) // 1 bool(false) // "" bool(true) // "Hello" bool(false) // [] bool(true) // [1, 2, 3] bool(false) // null bool(false) // 0.0 bool(true) // 3.14
Logical operators && and || use short-circuit evaluation. This means if the result is determined by the first operand, the second operand is not evaluated.
Example:
<?php $isUserLoggedIn = true; if ($isUserLoggedIn) { echo "Welcome, User!"; // This message will be displayed } else { echo "Please log in."; } ?>
true and false are case-insensitive, so True, FALSE, and fAlSe are valid but not recommended for consistency.
The above is the detailed content of Understanding PHP Types – Booleans. For more information, please follow other related articles on the PHP Chinese website!