Monday, 29 March 2021

Php: Emptiness

In Php "", "0", 0, 0.0, null, false, array() are considerd as empty.

 

How can I check the emptiness of a variable?

You can check the emptiness of a variable using empty() function.

 

emptiness_demo.php

#!/usr/bin/php

<?php
    $var1 = "";
    $var2 = '';
    $var3 = 0;
    $var4 = 0.0;
    $var5 = '0';
    $var6 = '0.0';
    $var7 = null;
    $var8 = false;
    $var9 = array();
    $var10 = [];
    $var11 = 1;

    echo (empty($var1)?'var1 is empty': "\$var1 : $var1")."\n";
    echo (empty($var2)?'var2 is empty': "\$var2 : $var2")."\n";
    echo (empty($var3)?'var3 is empty': "\$var3 : $var3")."\n";
    echo (empty($var4)?'var4 is empty': "\$var4 : $var4")."\n";
    echo (empty($var5)?'var5 is empty': "\$var5 : $var5")."\n";
    echo (empty($var6)?'var6 is empty': "\$var6 : $var6")."\n";
    echo (empty($var7)?'var7 is empty': "\$var7 : $var7")."\n";
    echo (empty($var8)?'var8 is empty': "\$var8 : $var8")."\n";
    echo (empty($var9)?'var9 is empty': "\$var9 : $var9")."\n";
    echo (empty($var10)?'var10 is empty': "\$var10 : $var10")."\n";
    echo (empty($var11)?'var11 is empty': "\$var11 : $var11")."\n";
?>

 

Output

$./emptiness_demo.php 

var1 is empty
var2 is empty
var3 is empty
var4 is empty
var5 is empty
$var6 : 0.0
var7 is empty
var8 is empty
var9 is empty
var10 is empty
$var11 : 1

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment