0
0
PHPprogramming~5 mins

Null type and its meaning in PHP

Choose your learning style9 modes available
Introduction

Null means a variable has no value. It shows that something is empty or missing.

When you want to clear a variable's value.
When a function returns no result.
When you want to check if a variable has been set or not.
When you want to represent 'no data' in a program.
Syntax
PHP
$var = null;

Use null to assign no value to a variable.

Null is case-insensitive but usually written in lowercase.

Examples
This sets variable $a to null, meaning it has no value.
PHP
$a = null;
This checks if $b is null exactly, then prints a message.
PHP
if ($b === null) {
    echo 'No value';
}
You can change a variable from a number to null to clear its value.
PHP
$c = 5;
$c = null; // Now $c has no value
Sample Program

This program sets a variable to null and checks if it is null. It prints a message based on that.

PHP
<?php

$var = null;

if ($var === null) {
    echo "Variable is null";
} else {
    echo "Variable has a value";
}

?>
OutputSuccess
Important Notes

Use === to check for null to avoid confusion with other falsey values like 0 or empty string.

Null means 'no value', not zero or empty string.

Summary

Null means a variable has no value.

You assign null using $var = null;.

Check null with === null for exact match.