When should I use Empty or isset?

Asked

Viewed 6,427 times

7

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

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

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 mean about the size and complexity of a code - I decided to elaborate these questions.

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

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

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

  • I want to know if a variable’s value $_SESSION exists, but this value should be true (whatever it is 1 or true, only needs to be a value equivalent to true).

  • I want to know if a arrayof results of a query to the bank came as array empty or not.

  • I want to know if a variable exists and if it doesn’t have the value equal to 0.

  • 4
  • In this case it speaks of the use of both (which in most cases is unnecessary by the codes I contemplate)

  • Take a look here, there is a table that shows what each one does differently: https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

  • is_null I would only use for callbacks, like array_filter. For "nail" check I find unnecessary;

  • Who do you say empty does not check missing variable : http://ideone.com/iP6V64

  • I believe that has already been answered: http://answall.com/questions/89960/como-validar-cada-datatype-received-de-um-formul%C3%A1rio? noredirect=1#comment182108_89960

Show 1 more comment

3 answers

7

The function names themselves already say it all, isset() to check if really the variable is set and prevent errors, Empty() is complicated, for you to really know if a value is empty (no spaces, null, etc.), you should always treat the variable, I always recommend using this lib https://github.com/Respect/Validation This answers your first two questions, let’s go next:

I want to know if a certain value of the $_SESSION variable exists, however this value must be true (whether it is 1 or true, it only needs to be one value equivalent to true).

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

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

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

I want to know if a variable exists and if it doesn’t have the value of 0

isset($var) && $var === 0

4

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

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

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

What should I consider when using the Empty function so that no undesirable conditions for code logic occur?

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

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

isset() You simply check if the value exists, or if it is defined that practically the same thing, you check whether that particular result exists, nothing more than that. It amounts to something that can be measured, or cause changes.

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

Using the isset() you can check if that same session has already been initialized, or if it has any value.

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

To array you can use the empty(), because usually when one(a) array comes without any value, therefore falls into the category of emptiness, simple.

I want to know if a variable exists and if it doesn’t have the value of 0.

For this, you should use both, first check if the variable received some value, although 0, zero is a null existence (The.o), it is, and then it must be checked whether or not its value is null.

Some examples of null values

$var = ""; # String vazia é NULL mas para o isset() está definida por ser String
$var = null; # null é NULL
$var = 0; # Zero é NULL
$var = 0.0; # 0.0 é NULL
$var = "0" # String 0 é NULL
$var = false; # é NULL
$var = array(); # Array vazia é NULL
Bits\Bytes Nulos também contam, é possível observar isso nalguns exemplos aqui

NOTE: You can always read my answer on link in the references, there I explained also a little about null values, and types of checks.


References:

Based on what I just answered, there may still be some doubts, there is understandable, because working with values of this type and similar functions is sometimes confusing, however, I recommend that you continue to look for other definitions, or explanations from different people, so that you can come to a conclusion, if you feel that these are not enough. Learn from it.

NULL (NULL) - Wikipedia

How to validate each data type received from a form?

3

To summarize, here’s exactly what you need to know. Where it is 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) |              |               |   
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   

Browser other questions tagged

You are not signed in. Login or sign up in order to post.