Curriculum
This tutorial will teach you about the PHP Boolean data type and boolean values.
A truth value is represented by a boolean value. To put it another way, a boolean value can be either true or false. To represent boolean values, PHP employs the bool type.
True and false keywords can be used to represent boolean literals. These are case-insensitive keywords. As a result, the following are also true:
And the following are equivalent to false:
When non-boolean values are used in a boolean context, such as an if statement. PHP converts the value to a boolean value. The following values are false when evaluated:
The keyword false
The integer zero (0)
The floating-point number zero (0.0)
The empty string (”) and the string “0”
The NULL value
An empty array, i.e., an array with zero elements
Other values are evaluated to true by PHP.
The following code demonstrates how to declare variables with Boolean values:
$is_submitted = false; $is_valid = true;
You can use the built-in function is_bool to determine whether a value is a Boolean (). For instance:
$is_email_valid = false; echo is_bool($is_email_valid);
When you use the echo function to display a boolean value, it will display 1 for true and nothing for false, which is counterintuitive. You can make it more obvious by using the var dump() function. As an example:
<?php $is_email_valid = false; var_dump($is_email_valid); $is_submitted = true; var_dump($is_submitted);
bool(false)
bool(true)