EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Booleans

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Booleans

PHP Booleans

Introduction to PHP Booleans

Before understanding what is PHP Boolean, let’s understand what is Boolean?

Boolean is a data type that is used in most computer programming languages like Java, Python, C, PHP, etc. It is a data type that has one or two possible values (either true or false). It is intended to represent the two truth values of logic and Boolean algebra. Depending upon the conditions it will set its value as 1(true) or 0(false). This data type is used by many programming languages to check if the condition satisfies and the statements get executed.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

PHP Boolean

In PHP, the boolean data type is used to set the values of the variables. It is mostly used for conditional statements like If, While, For, Switch, Etc. These conditional and iterative commands are mostly defined to test these boolean-valued expressions. Boolean value returns only two values i.e. either true or false. so, it is used in conditional statements to pass through that particular condition to execute the following statements corresponding to it.

Types of PHP Booleans Value

Let’s take a look at different types of boolean values:

  • Integer: This Boolean value is used to check the condition of whether the variable’s output is non-zero. If the output is zero, then the condition is false and the statements will not be executed presently inside the loop and will skip the loop and execute the further statements.
  • Floating Point: This Boolean value is used to check the condition of whether the variable’s output is a floating number for e.g. 0.0. If the output is non-zero, then the condition is true and the loop statements will be executed, if the output is zero then the statements inside the loop will be skipped and will proceed to execute the further statements.
  • Strings: This Boolean value is used to check whether the string is empty or not. If the output of the conditional statement is true, then the output will be a string value and the statements inside the loop will get executed. If the output is false, then the output is either a zero string or an empty string.
  • Array: This Boolean value is used to check whether an array has elements in it. If the condition is true, then it must be having at least one number of element and the statements inside the loop will get executed. If the condition is false, then the output must be an empty array and will skip the statements inside the loop and will proceed to execute the further statements.
  • NULL: This Boolean value is used to check whether the variable’s value is NULL. A variable’s value will be NULL if it is initialized to NULL at the beginning itself or it has not been set any value or it is unset. If the condition is true, then statements inside the loop will get executed. If the output is false, it will skip the statements inside the loop and will proceed to execute the further statements.
  • Objects: This Boolean value is used to check whether an object is present or not. If it is present, then the condition is true and the statements will be executed and the condition is false, then it will skip the statements inside the loop and will proceed to execute the further statements.

How Boolean Value Works?

Boolean values are nothing but 0 and 1 i.e. either true or false. if the condition satisfies, it is true else it is false.

Example #1

Let’s consider simple examples to understand how Boolean value works.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,731 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

<?php
$selling_price = 75.5;
$cost_price =50;
if ($selling_price == 0)
{
echo "The selling price should be a non zero";
}
else
{
echo "The selling price is $selling_price";
}
?>

Output:

PHP Booleans exampl1

In the above example, the output is a non-zero. Therefore, the statements inside if the statement is not executed.

Example #2

Let’s take another example for string Boolean value:

Code:

<?php
$a="Leela";
$b="Swamy";
if ($a)
{
echo "The name of the person is ".$a.$b;
}
else
{
echo "The string is empty";
}
?>

Output:

PHP Booleans exampl2

In the above example, the name is non-empty and also no comparison operator is used. PHP automatically converts the value and sets it to its Boolean equivalent true. So the statements will be executed written inside if statement.

Example #3

Let’s take another example:

Code:

<?php
$var=NULL;
$var1=500;
if ($var == NULL)
{
$var=$var1;
echo "The value of var is $var";
}
?>

Output:

p exampl3

In the above example, the $var variable has been initialized to null. So the condition becomes true and the statement written inside the loop gets executed and sets the value to 500.

Example #4

The function is_bool () can be used to check whether a variable contains a Boolean value or not. The is_bool () is an inbuilt function in PHP. It is a Boolean function so it returns TRUE when the value is a Boolean value, otherwise FALSE. Let’s take a simple example.

Code:

<?php
$a=TRUE;
$b=FALSE;
echo $a.'<br>';
echo $b.'<br>';
echo is_bool($a).'<br>';
echo is_bool ($b).'<br>';
?>

Output:

p exampl4

In the above example, the output of the function is_bool() will be either 0 or 1. In this example, the output will be 1 and after the break also it will be 1. It just checks whether the Boolean value is set to the variable and in this example, both the variables have been initialized Boolean values.

Example #5

Similar to is_bool () function, we have a function called var_dump () function to print both the type as well as the value as output. This function is used to print the data type associated with the variable that the developers want to print on the screen.

Code:

<?php
$a = true;
echo $a.'<br>';
echo is_bool($a).'<br>';
var_dump($a).'<br>';
?>

Output:

Booleans exampl5

In the above example, the output of is_bool() function and var_dump function() differs. The var_dump() function will print the data type along with the value of the variable.

Conclusion

In this article, we discussed the Boolean value and its types. Many of the Boolean types are used in many programs and applications. Boolean values play a vital role in many programming languages especially in PHP where we use cookies and sessions to check whether they are set or unset.

Recommended Articles

This is a guide to PHP Booleans. Here we discuss the basic concept with different types of PHP booleans, it’s working along with examples and code implementation. You may also look at the following articles to learn more –

  1. Palindrome in PHP
  2. Abstract Class in PHP
  3. Object in PHP
  4. PHP String Operators

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More