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

History of PHP: From PHP/FI to Modern Web Development

You use PHP every day if you build websites, but most people do not know where it came from or…

PHP array_pop: Remove and Return the Last Element of an Array

The array_pop function in PHP gives you a direct way to take the last element from an array. Understand the…

PHP MySQL LIMIT Data: How to Optimize PHP Queries?

There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…

PHP filter_var_array: How to Validate Multiple Inputs

The filter_var_array() appeared to make input validation simple and sanitization in PHP. It handles multiple inputs with different filters was…

PHP Elvis Operator: How (?:) Works with Examples

The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…

PHP compact Function: Assoc Array from Variables

The PHP compact function is a way to pull variables into an array. In this article, you will learn how…

PHP Anonymous Function: How It Works with Examples

The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…

PHP $_POST: Handling Form Data in PHP

The term $_POST is quite familiar territory when one works with forms in PHP. This tutorial talks about what $_POST does, how to use…

Traits in PHP: What They Are & Examples

PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…

Inheritance in PHP: Share Code from Class to Class & Examples

PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions 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.