PHP Null: How to Assign and Check for Null Values

PHP Null

PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists. Functions can return null when they fail or lack data. Objects can also have null properties if not initialized.

Let’s start with how its values work in PHP.

What Is Null in PHP?

PHP null represents a variable with no value. A variable is null if:

  • It is not assigned any value.
  • It is explicitly set to null.
  • Show unset within unset().

PHP treats null as a special type. That means it has no data stored in a variable when it is null.

Here is an example:

$variable = null;

var_dump($variable);

Here is the output:

NULL

You can use null value in the following cases:

  • Use null as a placeholder when a value is unknown or optional.
  • Return null from a public function if no result exists.
  • Use null for properties that may not have a value.
  • Assign null to free up memory or reset a variable.
  • Set object properties in PHP classes to null by default.

Let’s move on to the following section to understand how to check for null in PHP.

Check for Null in PHP

PHP provides you multiple ways to check if a variable is null:

  • How to use the is_null() built-in function.
  • Strict comparison.
  • Use the isset().
  • Check it within empty().

Let’s take each one in-depth.

Check for Null Within is_null()

The is_null() function returns true if the variable is null.

$variable = null;

if (is_null( $variable )) {  
    echo "Variable is null";  
}

Here is the ouput:

Variable is null

Check for Null Within Strict Comparison

The === operator checks if a variable is exactly null.

$var = null;  
if ($var === null) {  
    echo "Variable is null";  
}

Output:

Variable is null

Use isset()

PHP has isset() the built-in function to check if the is set and not null.

$var = null;  
if ( !isset($var) ) {  
    echo "Variable is null or not set";  
}

The output:

Variable is null or not set

Check Within empty().

The empty() function considers null as empty. It also treats the following values as empty:

  • 0
  • ""
  • false
$var = null;  
if (empty($var)) {  
    echo "Variable is empty or null";  
}

Here is output:

Variable is empty or null

Now lets move on to the below section to see the difference between null, undefined and empty.

Null vs Undefined vs Empty in PHP

PHP has three different concepts for missing or empty values: null, undefined, and empty. Each behaves differently.

Here is a table shows the key differences:

TypeWhen it OccursExampleCheck With
NullVariable exists but has no value$var = null;is_null($var) or $var === null
UndefinedVariable was never set$var;isset($var) (returns false)
EmptyVariable has a falsy value$var = "";empty($var)

But How to cast null into other types in PHP? In the following section, we will see the answer to that.

How to Cast Null in PHP?

PHP does not allow you to cast null value to other types using (int), (string), (bool), etc. However, you can convert null to other types using explicit casting or type conversion functions.

You can convert null to 0 when cast to an integer.

$var = null;  
$intValue = (int) $var;  
echo $intValue;

Output:

0

Here is another example to cast null value to string.

$var = null;  
$stringValue = (string) $var;  
echo $stringValue; 

Outputs:

(Program did not output anything!)

Wrapping Up

In this tutorial, you learned how PHP handles null and when to use it. You explored different ways to check for null:

  • Using is_null().
  • strict comparison (===).
  • Use the isset().
  • Use the empty().

You also saw the differences between null–undefined and empty values in PHP.

Finally, you learned how to cast null to other types like integers, strings, arrays, and objects.

Here’s a quick recap:

  • null represents a variable with no value.
  • Use is_null($var) or $var === null to check for null.
  • Use isset($var) to check if a variable exists and is not null.
  • Use empty($var) to check for null and other falsy values.
  • PHP allows casting null into integers, strings, arrays, and objects with type conversion.
  • In PHP classes, null can be set as default property values.

FAQ’s

What is null in PHP?

PHP, null represents a variable with no value. A variable is considered null if:​
    ​
  • It has not been assigned any value.​
  • It has been explicitly set to null.​
  • It has been unset using the unset() function.​
PHP treats null as a special data type indicating the absence of a value. ​

How can I check if a variable is null in PHP?

PHP provides multiple ways to check if a variable is null:​ Using is_null() function:
  $variable = null;
  if (is_null($variable)) {
      echo "Variable is null";
  }
Using strict comparison (===):
  $variable = null;
  if ($variable === null) {
      echo "Variable is null";
  }
Using isset() function: $variable = null; if (!isset($variable)) { echo "Variable is null or not set"; }

What is the difference between null, undefined, and empty in PHP?

  • null: A variable is null when it has been explicitly assigned null or has not been assigned any value.​
  • undefined: A variable is considered undefined if it has never been declared or assigned a value. Accessing such a variable will result in a notice-level error.​
  • empty: A variable is considered empty if it holds a falsy value, such as "" (empty string), 0 (integer zero), 0.0 (float zero), "0" (string zero), false, array() (empty array), or null. The empty() function can be used to check for these values.​

How do I set a variable to null in PHP?

You can set a variable to null by explicitly assigning null to it:​
$variable = null;
Alternatively, you can use the unset() function to destroy the variable, which will also set it to null:​
unset($variable);

How can I cast null to other data types in PHP?

To cast null to other data types results in the following:​
$variable = null;
  $intValue = (int) $variable;
  echo $intValue; // Outputs: 0

Similar Reads

PHP Comment: How to Write Comments in PHP

let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…

PHP File Inclusion: require, include, require_once, include_once

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

PHP array_change_key_case: Change Array Key Cases

The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…

PHP strtoupper Function: Convert Strings to Uppercase

Use strtoupper() function when you want to change all letters in a string to uppercase in PHP. It works with…

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 MySQL CRUD: Understanding Database Operations

PHP and MySQL have become an inseparable pair for web developers. One handles the logic, while the other stores the…

PHP Callable & Callback: Pass a Function to Another

The callback in PHP lets you pass functions as arguments and reuse code. It helps you to control how tasks…

PHP Multidimensional Arrays: Data Manipulation

In web development, managing and manipulating data is one of the most common tasks. When it comes to storing complex…

PHP Escape Characters: How to Escape Special Characters

Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…

How to Insert Data into MySQL with PHP

pplications. Such As as From registering new users to collecting form submissions and storing product details. Things like adding a…

Previous Article

Traits in PHP: What They Are & Examples

Next Article

Install PHP on Operating System: Ubuntu, Windows, and macOS

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.