How and when are isset () and empty () used correctly?

11

I have seen on many occasions when receiving the data of a form the following check:

if (isset($_POST['campo1'])) {
   // Resto de código
}

What happens if the $_POST['campo1'] value is empty?

Or on many other occasions I have seen the following check:

if (isset($_POST['campo1']) && !empty($_POST['campo1'])) {
   // Resto de código
}

Is it completely redundant to check the $_POST['campo1'] value at the same time with isset() and !empty() ?

So my question is: how and when are isset() and empty() used correctly?

    
asked by aldanux 06.11.2016 в 13:51
source

2 answers

13

According to the isset () documentation:

  

Returns true if the variable exists and has a value other than    null , false otherwise.

For example:

$var = ''; // Cadena vacía

if (isset($var)) { // <= true
    ... 
}

It will always return true even if the variable is empty and should be checked with the function empty() :

$var = ''; // Cadena vacía

if (!empty($var)) { // <= false

    // No está vacía (true)

} else {

    // Está vacía (false)
}

And when should you use isset() ?

As long as you want to verify that the variable exists, a good example would be if you want to know if a form has been sent :

<form method="post" action="">
    ...
    <input type="text" name="campo1" value="">
    <input type="submit" name="submit" value="Enviar formulario">
</form>

if (isset($_POST['submit']) {
  ...
}

According to the empty () documentation:

  

Returns false if the variable exists and has a non-empty value,   non-zero Otherwise it returns true .

With empty() the following expressions are considered as empty ( empty ) and return true :

"" (una cadena vacía)
0 (0 como un integer)
0.0 (0 como un float)
"0" (0 como un string)
NULL
FALSE
array() (un array vacío)
$var; (una variable declarada, pero sin un valor)

Important to know that a warning is not generated if the variable does not exist.

This means that empty() is essentially the concise equivalent of:

!isset($var) || $var == false .

Then it would be redundant if we check the same variable with isset() and empty() at the same time.

An example how you can use isset() and !empty() at same time when you want to receive data from a form:

<form method="post" action="">    
    <input type="text" name="campo1" value="">
    ...
    ...
    <input type="submit" name="submit" value="Enviar formulario">
</form>

if (isset($_POST['submit']) {

    if (!empty($_POST['campo1'])) {
        ...
    }
}
    
answered by 06.11.2016 / 13:51
source
2

Isset

Isset you use when you want to know

  

if a variable is defined and it is not NULL ..

Example, file1.php

 if ( isset($_POST['submit']))
    {
      // Acciones
    }

This can be useful if you want to validate that a file receives POST variables, since you could access it by url localhost / mifolder / file1.php , if you agree so you are not sending any value for POST, then it does not enter the If.

Another example:

$var = '';

// Esto evaluará a TRUE así que el texto se imprimirá.
if (isset($var)) {
    echo "Esta variable está definida, así que se imprimirá";
}

Source: isset documentation

Empty

Empty is to know if a variable is empty.

  

"A variable is considered empty if it does not exist or if its value is equal to   FALSE. empty () does not generate a warning if the variable does not exist. "

$var = 0;

// Se evalúa a true ya que $var está vacia
if (empty($var)) {
    echo '$var es o bien 0, vacía, o no se encuentra definida en absoluto';
}

What PHP considers as empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array () (an empty array)
  • $ var; (a declared variable, but without a value)

Source: empty documentation

Is it redundant to use isset together empty?

No, since there may be a variable defined but whose value is 0, then it is necessary to evaluate first that it is not null, and if it is not null evaluate that it is not empty in case you want to return a message to the user indicating that he must enter a value or if he entered it, indicating that it can not be 0 or a space, to say two examples (the format in general).

$var = 0;

if (isset($var))
  echo 'La variable está definida';

if (empty($var))
  echo 'La variable está definida pero está vacía';

Conclusion: isset and empty evaluate a variable in a different manner. Isset if a variable is null, empty if it is empty and can be NULL or any of the previous list.

    
answered by 09.11.2016 в 19:05