@
is not a good practice, although it may be used, what is recommended for doing variable checks is isset
.
isset
other than array_key_exists
(which can also be used as the example of Ricardo) supports multidimensional checks and you can also check more than one variable at a time, for example:
-
Checking 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 a 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
isset
also supports stdClass
(or variables of a class)
Details about the isset:
It is not a function, it is a constructor
If the variable is NULL
or FALSE
it will return false
, as in the example:
<?php
$a = NULL;
var_dump(isset($a)); //Retorna bool (false)
empty vs isset
You may not want to check multiple variables at the same time, as in the example above isset
( isset($a, $b, $c, $d, $e)
), you can use empty
, which in addition to verifying 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 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 since it is NULL)
The question problem
How to suppress all notifications of undeclared variables without using @
? In this case I would recommend using isset
and creating 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 sounds cumbersome, you can use a loop with foreach
or for
, it's a bit tricky to admit, but there are different ways to do the process, this is just one and it 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 isset
and 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 mentioned in the other answer, the use of error_reporting
is a good practice for the production and development environment.
I personally always use all "on" error notifications in the development environment:
<?php
//No topo do script
error_reporting(E_ALL|E_STRICT);
In the production environment I 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 do you 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 write errors to a .txt
file for example and refer to this file to check when the fault occurred, see an example in this other answer:
link
Note: It's not because you're going to turn off error messages that you should not use isset
or empty
, try to use E_ALL|E_STRICT
to detect possible code faults and then fix them.