The empty() function in Php

Admin   PHP   224  2021-02-02 08:41:11

The empty()  function is a function that specializes in checking empty data in PHP

Description of function empty()

bool empty($var)

The isset() function identifies the variable to be checked for unspecified or FALSE value

Parameters

The parameter of the empty function($var) only supports when $var is variable.

Return value of empty() function

Returns FALSE if the variable exists and is not empty, does not contain the value 0, otherwise returns TRUE

The following values ​​are considered empty:

  • ""  (String)
  • 0  (0 is an integer)
  • 0.0  (0 is a real number)
  • "0"  (0 is the string)
  • NULL
  • FALSE
  • array()  (which is an empty array)
  • $var;  (An undeclared variable, no value)

Note: The function empty($var) is equivalent to! Isset ($var) || $ var == false.

Incidentally isset($var) returns TRUE but empty ($var) returns FALSE. Therefore, during the establishment of the conditional expressions, it is necessary to define clearly to create the appropriate expression

For example

<?php

$error= "";

if(!empty($error)){

   echo 'The error variable has no value';

}


if(!empty($error)){

   echo $error;

}


?>

The empty() function is used in which case

  • Use to check if the variable value is empty or not
  • Serving for the normalization of data when manipulating to receive values ​​from the form