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
orisset
, 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 betrue
(whether it is1
ortrue
, 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
.