- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP Operators
- PHP - Operators
- PHP - Arithmatic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Quick Guide
- PHP - Useful Resources
- PHP - Discussion
PHP is_null() Function
PHP defines NULL as one of its special data types. It indicates that a certain variable has not been assigned a value any specific data type. It is a built-in constant in PHP and is used to indicate the intentional absence of any object or value. A variable can be explicitly assigned NULL or its value been set to null by using the unset() function.
The is_null() Function
PHP provides a Boolean function is_null() to check if a variable is indeed of NULL type.
is_null(mixed $value): bool
Example 1
If any variable is explicitly assigned NULL, obviously the is_null() function returns true.
<?php $x = NULL; echo "Variable \$x is null? "; var_dump(is_null($x)); ?>
It will produce the following output −
Variable $x is null? bool(true)
Example 2
If a variable with a certain value is unset, then too the is_null() function returns true, but with a warning
<?php $x = "Hello"; unset($x); echo "Variable \$x is null?\n"; var_dump(is_null($x)); ?>
It will produce the following output −
Variable $x is null? bool(true) PHP Warning: Undefined variable $x in /home/cg/root/89262/main.php on line 5
Example 3
Similarly, if you just declare a variable, without assigning any value to it, the is_null() function returns true with a warning −
<?php $y; echo "Variable \$y is null?\n"; var_dump(is_null($y)); ?>
It will produce the following output −
Variable $y is null? bool(true) Warning: Undefined variable $y in hello.php on line 9
Example 4
You can also use the equality operator (==) to check if a variable is NULL.
<?php $x = NULL; if ($x === NULL) { echo '$x is NULL'; } else { echo '$x is not NULL'; } ?>
It will produce the following output −
$x is NULL
Example 5
A null string "" is not considered equal to NULL. Hence, the is_null() function as well as the "==" operator return false. Take a look at the following example −
<?php $y = ""; if ($y === NULL) { echo '$y is NULL'; } else { echo '$y is not NULL'; } echo "$y is null?\n"; var_dump(is_null($y)); ?>
It will produce the following output −
$y is not NULL is null? bool(false)
Two other functions in PHP that are relevant to is_null() function are the isset() function and the empty() function.
The isset() Function
The isset() function determines if a variable is declared and is different than NULL.
isset(mixed $var, mixed ...$vars): bool
Example
A variable that is assigned NULL is considered as unset.
<?php $x = NULL; echo '$x is set? '; var_dump(isset($x)); ?>
It will produce the following output −
$x is set? bool(false)
Note that a null character ("\0") is not equivalent to the PHP null constant.
The empty() Function
The empty() function checks if a variable is considered to be empty. A variable is considered empty if it does not exist or if its value is NULL. empty() does not generate a warning if the variable does not exist.
Example 1
Take a look at the following example −
<?php $x = NULL; echo '$x is empty? '; var_dump(empty($x)); $y; echo '$y is empty? '; var_dump(empty($y)); ?>
It will produce the following output −
$x is empty? bool(true) $y is empty? bool(true)
Example 2
The empty() function returns true if a variable is set to "0", NULL, or is not set at all.
<?php $var = 0; if (empty($var)) { echo '$var is either 0, empty, or not set at all'; } ?>
It will produce the following output −
$var is either 0, empty, or not set at all