PHP Booleans
Before understanding what is PHP Boolean, let’s understand what is Boolean?
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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.
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:
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:
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:
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:
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:
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.
The above is the detailed content of PHP Booleans. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
