PHP empty() supports arbitrary expressions Tutorial

Udemy Generic 728x90

Definition:

This function checks whether a variable is empty or  not. In PHP5.5, instead of variables, arbitrary expression can be passed as an argument.

Syntax:

bool empty (mixed var )

parameter:

var :- an arbitrary expression or a variable to be checked

Description:

This function contains only one parameter,an arbitrary expression or a variable name. It checks whether an arbitrary expression or variable is empty or not.
If an arbitrary expression or variable  contains non-empty and non-zero value,then this function returns FALSE else it returns TRUE.

Usage:

  • In earlier versions of PHP only variable can pass as parameter in empty(),anything else will result in a parse error.
  • This will not work prior to PHP 5.5: empty(isEmpty()).Instead use isEmpty()==false.
  • An arbitrary expression or variable is considered to be empty, if it does not exist or if its value equals FALSE.
  • If the variable does not exist then no warning is generated by empty() and that means it(empty()) is equivalent to  !isset($variable) || $variable == false.

The things given below are considered to be empty:

  • An empty string i.e. “”
  • 0 as an integer i.e. 0
  • 0 as a float i.e. 0.0
  • 0 as a string i.e. “0”
  • NULL
  • FALSE
  • An empty array i.e. array()
  • A variable declared, but without a value i.e. $var
  • A function returns empty array or false.

isset() vs empty() vs is_null()

These three functions are very useful in PHP to check the variable status. The unexpected results will be given if these functions are not used in a correct manner. A boolean value is returned by these functions .Now Let’s understand the difference between them:

  • isset():- Checks whether the variable is set or not and it shouldn’t be null. If variable is not NULL only then it returns true.
  • is_null():- Checks whether the variable is NULL or not.If the variable is NULL only then it returns true.  
  • empty():- Checks whether the variable or arbitrary expression is empty or not.

Let’s understand with the help of an example given below:

<br />
&lt;?php<br />
     function is_Empty($emptyTestCases){<br />
         echo &quot;CHECK THE TEST CASES FOR EMPTY\n&quot; ;<br />
         foreach ($emptyTestCases as $key =&gt; $case ) {<br />
             if (empty($case)) {<br />
                 echo &quot;$key index is considered Empty: True.\n&quot;;<br />
             }else {<br />
                 echo &quot;$key index is considered Empty: False.\n&quot;;<br />
             }<br />
         }<br />
     } </p>
<p>    function is_Set($emptyTestCases){<br />
        echo &quot;\nCHECK THE TEST CASES FOR ISSET\n&quot; ;<br />
        foreach ($emptyTestCases as $key =&gt; $case ) {<br />
            if (isset($case)) {<br />
                echo &quot;$key index is set: True.\n&quot;;<br />
            }else {<br />
                echo &quot;$key index is set: False.\n&quot;;<br />
            }<br />
        }<br />
    } </p>
<p>    function isNull($emptyTestCases){<br />
        echo &quot;\nCHECK THE TEST CASES FOR NULL\n&quot; ;<br />
        foreach ($emptyTestCases as $key =&gt; $case ) {<br />
            if (is_null($case)) {<br />
                echo &quot;$key index is set to null: True.\n&quot;;<br />
            }else {<br />
                echo &quot;$key index is set to null: False.\n&quot;;<br />
            }<br />
        }<br />
    }</p>
<p>   $emptyTestCases = array (<br />
       0 =&gt; '',<br />
       1 =&gt; &quot;&quot;,<br />
       2 =&gt; &quot; &quot;,<br />
       3 =&gt; 0.0,<br />
       4 =&gt; 0,<br />
       5 =&gt; &quot;0&quot;,<br />
       6 =&gt; false,<br />
       7 =&gt;true,<br />
       8 =&gt; null,<br />
       9 =&gt; array(),<br />
       10 =&gt;&quot;\ 0&quot;,<br />
       11 =&gt; 'some value'<br />
   );</p>
<p>   echo is_Empty($emptyTestCases);<br />
   echo is_Set($emptyTestCases);<br />
   echo isNull($emptyTestCases);<br />
 ?&gt;<br />

Output:

CHECK THE TEST CASES FOR EMPTY
0 index is considered Empty: True.
1 index is considered Empty: True.
2 index is considered Empty: False.
3 index is considered Empty: True.
4 index is considered Empty: True.
5 index is considered Empty: True.
6 index is considered Empty: True.
7 index is considered Empty: False.
8 index is considered Empty: True.
9 index is considered Empty: True.
10 index is considered Empty: False.
11 index is considered Empty: False.
CHECK THE TEST CASES FOR ISSET
0 index is set: True.
1 index is set: True.
2 index is set: True.
3 index is set: True.
4 index is set: True.
5 index is set: True.
6 index is set: True.
7 index is set: True.
8 index is set: False.
9 index is set: True.
10 index is set: True.
11 index is set: True.
CHECK THE TEST CASES FOR NULL
0 index is set to null: False.
1 index is set to null: False.
2 index is set to null: False.
3 index is set to null: False.
4 index is set to null: False.
5 index is set to null: False.
6 index is set to null: False.
7 index is set to null: False.
8 index is set to null: True.
9 index is set to null: False.
10 index is set to null: False.
11 index is set to null: False.

Examples:

Example 1:

<br />
&lt;?php<br />
    function isEmpty() {<br />
        return false;<br />
    }<br />
    if (empty(isEmpty())) {<br />
        echo &quot;Function returns empty value.\n&quot;;<br />
    } else {<br />
        echo &quot;Function contains some value.\n&quot;;<br />
    }<br />
?&gt;<br />

Output:

   Function returns empty value.

Example 2:

<br />
&lt;?php<br />
    function emptyArray() {<br />
        $empty=[];<br />
        return $empty;<br />
    }<br />
    if(empty(emptyArray())) {<br />
        echo “Empty array is returned.\n”;<br />
    }<br />
?&gt;<br />

Output:

   Empty array is returned.

Example 3:
Find out the factorial and see the use of empty()

<br />
&lt;?php<br />
     function factorial($number){<br />
         if(empty($number)){<br />
             return 1;<br />
         }else{<br />
             return $number*factorial($number-1);<br />
         }<br />
     }</p>
<p>    echo &quot;Factorial of 4 is:&quot;.factorial(4);<br />
?&gt;<br />

Output:

   Factorial of 4 is:24

Example 4:
Please check the record of employees age whether they mentioned it or not.

<br />
&lt;?php<br />
     $employeesAge=array(&quot;Anil&quot;=&gt; &quot;&quot;,<br />
                         &quot;Udit&quot;=&gt; &quot;25&quot;,<br />
                         &quot;Dev&quot;=&gt; &quot;26&quot;);</p>
<p>     foreach($employeesAge as $name=&gt;$age){<br />
         if(empty($age)){<br />
             $age=&quot;Please set your Age!\n&quot;;<br />
             echo $name. &quot; &quot;.$age;<br />
         }else{<br />
             echo  $name.&quot; is &quot;. $age.&quot; years old.\n&quot;;<br />
         }<br />
     }<br />
?&gt;<br />

OutPut:

   Anil, Please set your Age!
   Udit is 25 years old.
   Dev is 26 years old.

Example 5:
How empty() function behaves when passing string offsets in different PHP versions.

<br />
&lt;?php<br />
    $expectedString = 'somestring';<br />
    var_dump(empty($expectedString['some_key']));<br />
    var_dump(empty($expectedString[0]));<br />
    var_dump(empty($expectedString['0']));<br />
    var_dump(empty($expectedString[0.5]));<br />
    var_dump(empty($expectedString['0.5']));<br />
    var_dump(empty($expectedString['0 Mostel']));<br />
?&gt;<br />

In PHP 5.5 and 5.4 output will be same.
Output:

   bool(true)
   bool(false)
   bool(false)
   bool(false)
   bool(true)
   bool(true)

In PHP 5.3
Output :

   bool(false)
   bool(false)
   bool(false)
   bool(false)
   bool(false)
   bool(false)

Example 6:
Check different test cases either empty or not by empty().

<br />
&lt;?php<br />
    function isTestCaseEmpty($value) {<br />
        if(is_array($value) and !empty($value)){<br />
            $tmp = array_shift($value);<br />
            if(!isTestCaseEmpty($value) or !isTestCaseEmpty($tmp))<br />
                return false;<br />
            else<br />
                return true;<br />
        }elseif(empty($value))<br />
            return true;<br />
        else<br />
            return false;<br />
    }</p>
<p>    $emptyTestCases = array (<br />
        0 =&gt; '',<br />
        1 =&gt; &quot;&quot;,<br />
        2 =&gt; 0.0,<br />
        3 =&gt; 0,<br />
        4 =&gt; &quot;0&quot;,<br />
        5 =&gt; false,<br />
        6 =&gt; null,<br />
        7 =&gt; array(),<br />
        8 =&gt; array(array()),<br />
        9 =&gt; array(array(), array(), array()),<br />
        10 =&gt; array(null),<br />
        11 =&gt; 'some value',<br />
        12 =&gt; array(array(&quot;not empty&quot;)),<br />
        13 =&gt; array(array(),array(&quot;not empty&quot;),array(array()))<br />
    );<br />
    foreach ($emptyTestCases as $key =&gt; $case ) {<br />
        echo &quot;array index $key is empty = &quot;.isTestCaseEmpty($case).&quot;\n&quot;;<br />
    }<br />
?&gt;<br />

Output:

   array index 0 is empty = 1
   array index 1 is empty = 1
   array index 2 is empty = 1
   array index 3 is empty = 1
   array index 4 is empty = 1
   array index 5 is empty = 1
   array index 6 is empty = 1
   array index 7 is empty = 1
   array index 8 is empty = 1
   array index 9 is empty = 1
   array index 10 is empty = 1
   array index 11 is empty = 
   array index 12 is empty = 
   array index 13 is empty = 

Here 1 represents empty and null represents non-empty.

Conclusion:

empty()  reduces the use of multiple condition checks and improve the code readability.

Udemy Generic 728x90

Spread the word. Share this post!