PHP Boolean: Assigning True or False to a Variable

You can assign the boolean data type to PHP variables or use it as a direct value. This enables you to represent the truth value. It is something either true or false. On other words, the boolean type is similar to the answer to a question with yes or no.

Additionally, we can use this type to handle any two choices during the program stack. In the next section, you will learn how to write boolean type values in various ways.

Boolean Type is Case Insensitive

The boolean type in PHP is case-insensitive and enables you to display the values of boolean ‘true’ or ‘false’ in various cases such as uppercase or lowercase without changing their meaning or functionality.

So, “true or false” is similar to “TRUE and FALSE” or “True and False”. For example:

$bool = True; // Assign the value TRUE to $bool

There is a built-in callback in PHP to check if the current variable has a boolean data type or not. Let’s take a look at it in the section below.

Checking for Boolean Values

PHP has variety of built-in functions for detecting specific data types, such as is_bool function. The is_bool function determines whether a given variable is of the boolean data type. So if the value is not Boolean it will show “false”.

Here is an example:

<?php  
$variable1 = true;
$variable2 = 42; // Not a boolean
$variable3 = "false"; // Not a boolean

// Check if $variable1 is a boolean
if (is_bool($variable1)) {
    echo '$variable1 is a boolean.';
} else {
    echo '$variable1 is not a boolean.';
}

// Check if $variable2 is a boolean
if (is_bool($variable2)) {
    echo '$variable2 is a boolean.';
} else {
    echo '$variable2 is not a boolean.';
}

// Check if $variable3 is a boolean
if (is_bool($variable3)) {
    echo '$variable3 is a boolean.';
} else {
    echo '$variable3 is not a boolean.';
}

In this example, $variable1 is a boolean, so the first condition is true, and it prints that $variable1 is a boolean. However, for $variable2 and $variable3, which are not booleans, so, the conditions are false, and it prints that they are not booleans.

This function is useful when you need to validate or check the type of a variable in your PHP code, especially when dealing with user input or external data.

Anyway, let’s explore how to use the Boolean data type in control structures.

PHP Boolean Values in Control Structures

PHP is a flexible language. When you need to check if a variable has a value, but you are not certain about its existence, you can use an if statement to determine whether the variable has a value or not. This process automatically yields a boolean value.

Here is an example:

<?php
  
  $value = "CodedTag";
  
  // true statement ( It has a value )
  if( $value ) {
    echo "This condition yields a truth value.";
  }
  
?>

Or you can use the boolean value directly.

<?php
  
  $value = "CodedTag";
  
  // true statement ( It has a value )
  if( $value == true ) {
    echo "This condition yields a truth value.";
  }
  
?>

Anyway, let’s summarize.

Wrapping Up

In this tutorial, you have understood what the PHP boolean data type is. Here are a few points to summarize this guide:

  • A PHP boolean is a logical data type that has two values: true and false.
  • True and false are case-insensitive, which allows you to write uppercase or lowercase.

Thank you for reading. Happy Coding!

Similar Reads

PHP array_rand() Function: Usage & Examples

The array_rand() is a helper function in PHP that makes it easier when dealing with arrays; you can just pull…

PHP file_exists: Check File and Directory Existence

Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…

PHP While Loop: Iterating Through Code Blocks

The PHP “while” loop is a simple tool that repeatedly runs a block of code as long as a certain…

PHP $_FILES: How to Upload Files in PHP

The PHP superglobal $_FILES is a very important utility and great for dealing with file uploads on the web. When a user…

PHP implode Function: Join Array Values into a String

The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…

PHP array_keys: How to Extract Keys in Arrays with Examples

This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

PHP array_find: How to Locate Array Values with Examples

PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…

PHP fread Function: How to Read Files in PHP

In some cases, you need to handle a lot of data or simply try to open a file, or read…

Understanding PHP Constants: A Simple Guide with Examples

In regard to the use of constants in PHP, clear understanding of their definition and use could be critical in…

Previous Article

PHP Type Hints: Ensuring Data Integrity

Next Article

PHP Strings: Types, Variables & Syntax Tips

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.