CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PHP - Boolean

PHP - Boolean

Boolean Type in PHP

In PHP, “bool” is a basic data type. It informs if something is true or not. A Boolean can only have two values: true or false. True and false in PHP can be written in several ways, such as true, TRUE, or True, and they all mean the same thing. Boolean values help you make decisions in your code.

For example, you can tell if something is correct (true) or incorrect (false). This can be used in conditions, loops and other PHP programming areas to control how the code executes based on true or false values.

The following is the list of topics discussed in this chapter −

  • Syntax to declare a Boolean Type
  • Boolean Values in PHP Conditions
  • Boolean Conversion in PHP
  • Boolean Operators in PHP
  • Strict Comparison with Boolean
  • Boolean in PHP Functions
  • Boolean Arrays in PHP
  • Boolean Constants in PHP
  • Practical Applications of Boolean

Syntax to declare a Boolean Type

Here is the way you can declare a variable of bool type as follows −

PHP CODE
// Boolean true
   $bool1 = true;   

   // Boolean true
   $bool2 = True;   
   
   // Boolean false
   $bool3 = FALSE;  
   
   // Boolean false
   $bool4 = false;

Example

Logical operators (<, >, ==, !=, etc.) return Boolean values.

PHP CODE
&lt;?php
   $gender="Male";
   var_dump ($gender=="Male");
?&gt;

It will produce the followingoutput−

PHP CODE
bool(true)

Example

Here is the simple example in which the variable $is_sunny is set to true. The if statement checks if $is_sunny variable is true. As it is true so it will print ‘It is a sunny day!‘.

PHP CODE
&lt;?php
   $is_sunny = true;

   if ($is_sunny) {
      echo "It is a sunny day!";
   } else {
      echo "It is not sunny today.";
   }
?&gt;

Here is theoutputof the above program −

PHP CODE
It is a sunny day!

Boolean Values in PHP Conditions

Boolean values are used in the construction of control statements such asif, while, forandforeach. The behaviour of these statements depends on the true/false value returned by the Boolean operators.

The following conditional statement uses the Bool value returned by the expression in the parenthesis in front of theifkeyword −

PHP CODE
&lt;?php
   $mark=60;

   if ($mark &gt; 50)
      echo "pass";
   else
      echo "fail";
?&gt;

Following is theoutputof the above program −

PHP CODE
pass

Boolean Conversion in PHP

Use the (bool) casting operator to convert a value to bool. When a value is used in a logical context it will be automatically interpreted as a value of type bool.

A non-zero number is considered as true, only 0 (+0.0 or -0.0) is false. Non-empty string represents true, empty string "" is equivalent to false. Similarly, an empty array returns false.

Example

Take a look at this following example −

PHP CODE
&lt;?php
   $a = 10;
   echo "$a: ";
   var_dump((bool)$a);

   $a = 0;
   echo "$a: ";
   var_dump((bool)$a);

   $a = "Hello";
   echo "$a: ";
   var_dump((bool)$a);

   $a = "";
   echo "$a: ";
   var_dump((bool)$a);

   $a = array();
   echo "$a: ";
   var_dump((bool)$a);
?&gt;

It will produce the followingoutput−

PHP CODE
10: bool(true)
0: bool(false)
Hello: bool(true)
: bool(false)
Array: bool(false)

Boolean Operators in PHP

Logical operators in PHP are symbols or words which perform logical operations on values that are either TRUE or FALSE. They are used to check logical conditions and manipulate boolean values.

PHP CODE
&lt;?php
   $a = true;
   $b = false;

   // AND operator
   var_dump($a &amp;&amp; $b);  

   // OR operator
   var_dump($a || $b);  

   // NOT operator
   var_dump(!$a); 
?&gt;

It will generate the followingoutcome−

PHP CODE
bool(false)
bool(true)
bool(false)

Strict Comparison with Booleans

A strict comparison with Boolean is the use of the strict equality operator (===) to compare values, that not only the values themselves but also their data types are equal and always returning a Boolean result of true or false.

PHP CODE
&lt;?php
   $val1 = true;
   $val2 = 1;

   var_dump($val1 == $val2);  
   var_dump($val1 === $val2); 
?&gt;

It will produce the belowoutput−

PHP CODE
bool(true)
bool(false)

Boolean in PHP Functions

PHP also provide some built in functions for boolean values. These functions are is_bool(), empty(), isset() etc. So we are showing the example of is_bool() which is used to check that the value is boolean or not and isset() which is used to check the value is set or not.

PHP CODE
&lt;?php
   $is_bool = is_bool(true);  
   var_dump($is_bool);

   // false if $name is not defined
   $isset = isset($name);  
   var_dump($isset);
?&gt;

It will produce the followingoutput−

PHP CODE
bool(true)
bool(false)

Boolean Arrays in PHP

Let us see if there is an array which contains boolean values and display the items of array using a loop.

PHP CODE
&lt;?php
   $boolArray = [true, false, true];
   foreach ($boolArray as $value) {
      var_dump($value);
   }
?&gt;

It will generate the belowoutput−

PHP CODE
bool(true)
bool(false)
bool(true)

Boolean Constants in PHP

In PHP, Boolean Constants are used to make logics and conditions. PHP offers two types of Boolean Constants true and false. Let us see an example below −

PHP CODE
&lt;?php
   const STATUS = true;
   if (STATUS) {
      echo "Status is active!";
   }
?&gt;

It will produce the followingoutput−

PHP CODE
Status is active!

Practical Applications of Booleans

Real-world example where Boolean values are used, like user authentication, form validation and feature toggles. Here is an example where we will get the status of the user is logged in or not.

PHP CODE
&lt;?php
   $user_logged_in = true;
   if ($user_logged_in) {
      echo "Welcome, User!";
   }
?&gt;

Here is theoutputthe above code −

PHP CODE
Welcome, User!