PHP NULL


Definition and Usage

In PHP, a variable with no value is said to be of null data type. Such a variable has a value defined as NULL. A variable can be explicitly assigned NULL or its value been set to null by using unset() function.

Syntax

$var=NULL;

It is possible to cast variable of other type to null, although casting null to other type has been deprecated from PHP 7.2. In earlier versions, casting was done using (unset)$var syntax

Following example shows how to assign NULL to a variable

Example

 Live Demo

<?php
$var=NULL;
var_dump($var);
?>

Output

This will produce following result −

NULL

Following example performs null variable to other primary variables

Example

 Live Demo

<?php
$var = NULL;
var_dump( (int) $var);
var_dump((float)$var);
var_dump((bool) $var) ;
var_dump( (boolean) $var);
?>

Output

This will produce following result −

int(0)
float(0)
bool(false)
bool(false)

Updated on: 19-Sep-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements