The @
is not a good practice, although it can be used, the recommended to do variable checks is the isset
.
The isset
other than array_key_exists
(which can also be used as Ricardo’s example) supports multidimensional checks and you can also check more than one variable at the same time, for example:
Checking for multidimensional arrays:
<?php
$test = array('a' => 1);
var_dump(isset($test['a']['b'])); //Retorna bool (false)
var_dump(isset($test['a'])); //Retorna bool (true)
Checking more than one variable at the same time:
<?php
$a = 1;
var_dump(isset($a)); //Retorna bool (true)
var_dump(isset($a, $b)); //Retorna bool (false) por que $b não existe
or
<?php
$a = array('test' => 1);
$b = array('foo' => 2);
var_dump(isset($a['test'])); //Retorna bool (true)
var_dump(isset($a['test'], $b['test'])); //Retorna bool (false) por que $b['test'] não existe
The isset
also supports stdClass
(or variables of a class)
Details about the isset:
- It’s not a function, it’s a builder
If the variable is NULL
or FALSE
he will return false
, as in the example:
<?php
$a = NULL;
var_dump(isset($a)); //Retorna bool (false)
Empty vs isset
Should it not be possible to verify several variables at the same time, as in the example already cited with isset
(isset($a, $b, $c, $d, $e)
), you can use the empty
, that besides checking if the "variable exists" it also checks if it is empty in case of strings for example. Situations that Empty will return TRUE
:
""
(an empty string)
0
(when it is an integer equal to zero)
"0"
(zero as string)
NULL
FALSE
array()
(an empty array)
var $var;
(When a variable is declared in a class, but has no value as it is NULL)
The problem of the question
How to delete all undeclared variable notifications without using @
? In this case I would recommend to use the isset
and create normal variables, such as:
$variavelA = empty($_SESSION['varA']) ? NULL : $_SESSION['varA'];
$variavelB = empty($_SESSION['varB']) ? NULL : $_SESSION['varD'];
$variavelC = empty($_SESSION['varC']) ? NULL : $_SESSION['varC'];
$variavelD = empty($_SESSION['varD']) ? NULL : $_SESSION['varD'];
If it seems laborious, you can use a loop with foreach
or for
, it is a little gambiarra admit, but there are different ways to do the process, this is just one and will depend on how your code is:
$keys_in_session = array('a', 'b', 'user', 'ultimaatividade');
foreach ($keys_in_session as $k => $v) {
if (empty($_SESSION[$v])) {//Acaso a variável não exista na sessão ele cria ela como o valor `NULL` para que você possa usa-la sem ocasionar
$_SESSION[$v] = NULL;
}
}
Consideration of use
Despite the example with foreach
, I personally recommend using only the isset
and the empty
combined with if
or with ternary comparison operators:
echo 'Resposta: ', ( empty($_SESSION['message']) ? 'sem resposta' : $_SESSION['message'] );
Development Environment vs Production Environment
As already quoted in the other answer, the use of error_reporting
is a good practice for the production and development environment.
Production environment is one that is understood at the moment that the software is already used to "produce something", in other words, when the site is already on the air.
Development environment is that of when you are developing the software in a place where it does not affect the server/ site or be on your machine for example.
I personally always use in development environment all error notifications "on":
<?php
//No topo do script
error_reporting(E_ALL|E_STRICT);
In production environment always turn off all errors:
<?php
//No topo do script
error_reporting(0);
But it is not possible to determine all failures that may occur and all situations, so how to know which errors occurred? For this we have 3 functions that we can use combined, error_get_last()
, set_error_handler
and register_shutdown_function
, so you can record the errors in a file .txt
for example and refer this file to check when the failure occurred, see an example in this other response:
/a/34818/3635
Note: It is not why you will turn off error messages that you should not use isset
or empty
, seek to use the E_ALL|E_STRICT
to detect possible code failures and then fix them.
In practice I would be taking something that is not good and exchanging it for something worse. It is right to correct and avoid common and predictable mistakes. The error inhibitor @ is used for very specific cases as functions that return inexperienced errors that cannot be controlled by the error setting.
– Daniel Omine
@gmsantos Why was my question edited and my greeting removed? I don’t recall reading anything that prohibits it, sorry if I’m wrong, but I found it unnecessary for you to interfere...
– Dirty Old Man
@Dirtyoldman this type of greeting is considered a noise. One of the slogans of SOPT is Ask questions, get answers, no distractions. More details on that goal response.
– gmsantos
I’m sorry if I seemed intrusive, but I have a habit of do that kind of editing :)
– gmsantos