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?
I can use Empty and isset in a variable?
– rray
In this case it speaks of the use of both (which in most cases is unnecessary by the codes I contemplate)
– Wallace Maxters
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/
– Ivan Ferrer
is_null
I would only use for callbacks, likearray_filter
. For "nail" check I find unnecessary;– Wallace Maxters
Who do you say
empty
does not check missing variable : http://ideone.com/iP6V64– Wallace Maxters
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
– Edilson