AppDividend
Latest Code Tutorials

PHP empty Example | empty() Function In PHP Tutorial

0

PHP empty Example | empty() Function In PHP Tutorial is today’s topic. The empty() function’s use is to determine whether the variable is considered to be empty. The variable is considered an empty if it does not exist or if its value equals FALSE. The empty() does not generate a warning if the variable does not exist. The empty() function accepts a single parameter as shown in the above syntax and described below. It’s opposite from PHP isset() function we have seen earlier.

PHP empty()

The empty() function is used to check whether the variable is empty or not. See the following syntax.

bool empty ($var)

The $var is the variable being checked and it is the required parameter.

Below version of PHP 5.5, empty() only supports variables, anything other will result in a parse error. The following statement will not work empty(trim($var)). Instead, use trim($name) == false.

Return Value: It returns FALSE when $var exists and has a non-empty, non-zero value. Otherwise, it returns TRUE.

These values are considered to be as an empty value:

  1. “” (an empty string)
  2. 0 (0 as an integer)
  3. 0.0 (0 as a float)
  4. “0” (0 as a string)
  5. NULL
  6. FALSE
  7. array() (an empty array)

See the following examples.

#empty() function with string

See the following code.

<?php

$mage = '';

if(empty($mage)) {
  echo 'It\'s empty';
}
else {
  echo 'Eleven is full';
}

See the output.

➜  pro php app.php
It's empty                                                                      
➜  pro

If a variable is not defined or does not exist, it will be considered as empty.

If you have declared a variable and not given any value, this will still be considered as empty.

#empty() function with integer

See the following code.

<?php

$num = 0;

if(empty($num)) {
  echo 'It\'s empty';
}
else {
  echo 'It is not';
}

See the output.

➜  pro php app.php
It's empty                                                                     
 ➜  pro

#empty() function with Array

See the following code.

<?php

$num = [];

if(empty($num)) {
  echo 'It\'s empty';
}
else {
  echo 'It is not';
}

See the output.

➜  pro php app.php
It's empty                                                                      
➜  pro

An empty array is also taken as “empty”.

#empty() Function with Boolean

See the following code.

<?php

$app = FALSE;

if(empty($app)) {
  echo 'It\'s empty';
}
else {
  echo 'It is not';
}

See the output.

➜  pro php app.php
It's empty                                                                      
➜  pro

If a Boolean variable is False, this is also considered as empty.

#empty() Function with Object

Let’s define the empty class and create an object of it and then check it with empty() function.

<?php

class App {

}

$data = new App();

if(empty($data)) {
  echo 'Class is empty';
}
else {
  echo 'Class is not empty';
}

See the output.

➜  pro php app.php
Class is not empty                                                              
➜  pro

That means that $data is not empty and some properties are there.

A variable with NULL value is also taken as empty.

#Complete Example

See the following complete example.

<?php

$varA = 0; 
$varB = 0.0; 
$varC = "0"; 
$varD = NULL; 
$varE = false; 
$varF = array(); 
$varG = ""; 
 
empty($varA) ? var_dump("True\n") : var_dump("False\n"); 

empty($varB) ? var_dump("True\n") : var_dump("False\n"); 

empty($varC) ? var_dump("True\n") : var_dump("False\n"); 

empty($varD) ? var_dump("True\n") : var_dump("False\n"); 

empty($varE) ? var_dump("True\n") : var_dump("False\n"); 

empty($varF) ? var_dump("True\n") : var_dump("False\n"); 

empty($varG) ? var_dump("True\n") : var_dump("False\n");

See the following output.

➜  pro php app.php
string(5) "True
"
string(5) "True
"
string(5) "True
"
string(5) "True
"
string(5) "True
"
string(5) "True
"
string(5) "True
"
➜  pro

Finally, PHP empty Example | empty() Function In PHP Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.