PHP Boolean
In this chapter you will learn:
- What is boolean value in PHP
- What values are considered false
- Example - Use PHP boolean value
- Example - Convert integer to boolean
Description
Booleans hold either true or false. Behind the scenes, booleans are integers.
False value
In addition, PHP considers the following values to be false
:
- The literal value false
- The integer zero (0)
- The float zero ( 0.0 )
- An empty string ( " " )
- The string zero ( "0" )
- An array with zero elements
- The special type null (including any unset variables)
- A SimpleXML object that is created from an empty XML tag
All other values are considered true in a Boolean context.
Example 1
We can assign true or false value to a boolean type variable.
<?PHP/* j a v a 2s. c o m*/
$bool = true;
print "Bool is set to $bool\n";
$bool = false;
print "Bool is set to $bool\n";
?>
The code above generates the following result.
Example 2
In the second if statement we compare the integer value to a boolean value.
<?PHP/* j av a 2s . co m*/
$a=100;
if($a==100) {
echo "the variable equals 1!\n";
}
if($a==true) {
echo "the variable is true!";
}
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: