Monday, 29 March 2021

Php: NULL: Represent nothing

‘NULL’ represent nothing, not having a value. Since the nature of case insensitivity, ‘NULL’ can be written in lowercase like null or in uppercase like NULL.

 

How to check whether variable is null or not?

‘is_null’ function takes a variable as argument and return true, if the variable do not point to anything, else false.

 

null_check_demo.php

#!/usr/bin/php

<?php
    $var1 = null;
    $var2 = NULL;
    $var3 = NUll;

    $var4 = 10;

    echo (is_null($var1)?'var1 is null': "\$var1 : $var1")."\n";
    echo (is_null($var2)?'var1 is null': "\$var2 : $var2")."\n";
    echo (is_null($var3)?'var1 is null': "\$var3 : $var3")."\n";
    echo (is_null($var4)?'var1 is null': "\$var4 : $var4")."\n";
?>

 

Output

$./null_check_demo.php 

var1 is null
var1 is null
var1 is null
$var4 : 10

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment