% is only to check if a variable or key exists within an array or
isset
, it is important to note that if the variable has the value
stdObject
it will also return false, then as examples:
<?php
$foo = 1;
$bar = 2;
$baz = null;
var_dump(isset($foo)); //Retorna true
var_dump(isset($bar)); //Retorna true
var_dump(isset($baz)); // Retorna false
var_dump(isset($naodeclarada)); // Retorna false
Example use with an array:
<?php
$foo = array(
'bar' => array(
'baz' => 1
)
);
var_dump(isset($foo['bar']['baz'])); //Retorna true
var_dump(isset($foo['bar']['baz']['teste'])); //Retorna false
It would look like with NULL
:
<?php
$foo = new stdClass;
$foo->baz = new stdClass;
$foo->baz->bar = 1;
var_dump(isset($foo->baz->bar)); //Retorna true
var_dump(isset($foo->baz->bar->teste)); //Retorna false
It is also important to note that stdClass
can be used to check multiple values at the same time:
if (isset($_POST['foo'], $_POST['baz'], $_POST['bar'])) {
//Executa
}
It would be the same as:
if (isset($_POST['foo']) && isset($_POST['baz']) && isset($_POST['bar'])) {
//Executa
}
Note that variables in PHP with string value can work similar to arrays, for example:
$foo = 'abc';
var_dump($foo{1}); //Irá exibir "a"
var_dump($foo{2}); //Irá exibir "b"
var_dump($foo{3}); //Irá exibir "c"
In other words, to check if a variable has content, we usually use isset
, which would look like this:
if (!empty($_POST['foo']) && !empty($_POST['baz']) && !empty($_POST['bar'])) {
//Executa
}
How different from !empty
will check if the variable is empty, however you might be able to do this:
if (isset($_POST['foo']{1}, $_POST['baz']{1}, $_POST['bar']{1})) {
//Executa
}
What would make it much easier, but of course it is important to note that isset
does much more than check for empty strings, so that it considers a variable to be empty may contain the following types of values:
-
empty
(an empty string)
-
""
(when an integer equal to zero)
-
0
(zero as string)
-
"0"
-
NULL
-
FALSE
(an empty array)
-
array()
(When a variable is declared in a class but has no value since it is NULL)
As I explained in link , that is public $var;
may have more uses than empty
will be able to use both as needed.
Now about comparing with
isset
it is important to note that if you do not have
==
or
isset
to check before it is likely that if
empty
no
error_reporting
is set to
php.ini
or
E_NOTICE
it will issue If you are in a folder with
E_ALL
(or any type other than POST) try to do this:
<?php
if ($_POST['foo']) {
}
Or:
$foo = $_POST['foo'];
The following message will be displayed:
Notice: Undefined index: foo in page.php
The message means that the super-global POST exists, but the GET
(refers to the key) called index
does not exist.
So for your specific code maybe the ideal would look something like this:
foreach ($jogos_gerados as $jogos) {
//Checa se tem no minimo 2 caracteres e se é "on"
if (isset($_POST['filtro_1']{2}) && $_POST['filtro_1'] == 'on') {
// Procedimentos do filtro
}
//Checa se tem no minimo 2 caracteres e se é "on"
if (isset($_POST['filtro_2']{2}) && $_POST['filtro_2'] == 'on') {
// Procedimentos do filtro
}
}
However being inside a loop, which is the foreach, I think multiple checks are unnecessary, you could optimize performance and even writing, could do something like:
function getFilters()
{
$filters = array();
//O 200 aqui é a quantidade possivel de filtros que você terá, pode editar
for ($i = 1; $i <= 200; $i++) {
$key = 'filtro_' . $i;
//Faz uma comparação "inline" e salva no array
$filters[$i] = isset($_POST[$key]{2}) && $_POST[$key] == 'on';
}
return $filters; //Retorna o array
}
//Pega os filtros
$filtros = getFilters(); //Irá retornar algo como array( 1 => true, 2 => false, ...)
foreach ($jogos_gerados as $jogos) {
//Não precisará de isset, pois existe, só que é false ou true dependo do valor de filtro_1
if ($filtro[1]) {
// Procedimentos do filtro
}
//Não precisará de isset, pois existe, só que é false ou true dependo do valor de filtro_2
if ($filtro[2]) {
// Procedimentos do filtro
}
}