When should I use empty or isset?

6

I noticed that some people have doubts about the use of these two functions, or if they have no doubts, they seem to unnecessarily apply one of these functions, and in some cases the other functions better.

For example, to verify that a value does not exist or if it is false, some people would do this:

if ( ! isset($var) || $var == false)
{
   // Não existe ou é false
}

Being that it could be done simply:

if (empty($var)) {
  // Não existe ou é falsa
}

Thinking about these types of complications that we generate - I say in relation to the size and complexity of a code - I decided to elaborate these questions.

  • In what cases, of the most common cases, could I use empty or isset , as a way to simplify how to check if a value is actually empty?

  • What should I consider when using the empty function so that undesirable conditions for code logic do not occur?

To help with the question, I have cases of elaborate uses, so we can apply the answers - using empty or isset .

  • I want to know if a certain value of the $_SESSION exists, but this value must be true (whether it is 1 or true , only needs to be a value equivalent to true) / p>

  • I want to know if a array of results from a query to the database came as% empty% or empty.

  • I want to know if a variable exists and if it does not have the value equal to array .

asked by anonymous 07.10.2015 / 21:53

3 answers

4

The function names themselves say everything, isset () to check if the variable is actually defined and prevent errors, empty () is complicated, so you really know if a value is empty (without spaces, null, etc.) , you should always treat the variable, I always recommend using this lib link this answers your first two questions, let's next: / p>

  

I want to know if a value of $ _SESSION exists, but   this value must be true (whether it's 1 or true, it just needs to be one   value equivalent to true).

isset($_SESSION['var']) && $_SESSION['var'] === true
  

I want to know if an array of results from a query to the database came   as empty array or not.

is_array($var) && !empty($var)
  

I want to know if a variable exists and if it does not have a value of 0

isset($var) && $var === 0
    
07.10.2015 / 22:49
2

To summarize, here is exactly what you need to know. Where empty, it will return bool(false) :

| valor com variável ($var) | isset($var) | empty($var) | is_null($var) |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| "" (string vazia)         | bool(true) | bool(true)   |               |                |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| " " (espaço)              | bool(true) |              |               |   
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| FALSE (boleano)           | bool(true) | bool(true)   |               |   
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| TRUE  (boleano)           | bool(true) |              |               |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| array() (vetor vazio)     | bool(true) | bool(true)   |               |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| NULL (nulo)               |            | bool(true)   | bool(true)    |   
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| "0" (0 como string)       | bool(true) | bool(true)   |               |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
|  0 (0 como inteiro)       | bool(true) | bool(true)   |               |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| 0.0 (0 como um float)     | bool(true) | bool(true)   |               |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| var $var; (a variável     |            |              |               |
|            declarada      |            | bool(true)   | bool(true)    |
|       sem definir valor)  |            |              |               |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
| NULL byte ("\ 0")         | bool(true) |              |               |   
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
    
07.10.2015 / 22:25
2

Well, for this I will select, and answer the questions one by one.

  

In which cases, of the most common cases, could I use empty or isset, as a way to simplify how to check if a value is really empty?

As for this it is really a bit complicated to explain, even because usually empty can have several meanings, from "vacant" to "hollow" . In PHP and in other languages, the logic and meaning is the same.

  

What should I consider when using the empty function so that undesirable conditions for code logic do not occur?

The only thing you should consider when using the empty() function is that the variable, or expected return, has value #.

07.10.2015 / 22:58