0
How do I count how many $_GET parameters are filled in? I made a Count() in $_GET but it also counts the amount of empty values.
0
How do I count how many $_GET parameters are filled in? I made a Count() in $_GET but it also counts the amount of empty values.
1
You can do a simple function to get the empty values:
<?php
function count_sem_vazios($array) {
$contagem = 0;
foreach ($array as $foo) {
if (!empty($foo))
$contagem++;
}
return $contagem;
}
// Demonstração:
$teste = array(
null, // essa NÃO conta
0, // essa NÃO conta
'string', // essa conta (1)
10, // essa conta (2)
true, // essa conta (3)
'' // essa NÃO conta
);
$contagem = count_sem_vazios($teste); // Retorna: 3
print_r($contagem);
// Para a variável $_GET:
// $contagem = count_sem_vazios($_GET);
Recalling that the Empty() has some considerations for emptiness:
""
(an empty string)0
(0 as a whole)0.0
(0 as a floating point)"0"
(0 as a string)NULL
FALSE
array()
(an empty array)$var;
(a declared variable but no value)1
You can use the function array_filter
to remove the empty values and then count the returned result.
$valoresVaziosGet = array_filter($_GET);
$contagemVaziosGet = count($valoresVaziosGet);
Thanks for your help
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Thank you Lipespry, I will study a little more about Empty()
– ElvisP
In fact, you don’t have to focus too much on Empty. I just emphasized the cases that he considers empty. You should use a formula that matches the content you want to filter. You can even create a callback and use it in conjunction with colleague @rcs' proposal that will work very well! Each case is a case. ;)
– LipESprY