There is a difference, but I do not know if it applies exactly to the case presented. The difference between using !(condition)
and condition === false
is that PHP naturally considers some values, other than false
, to be false. Some of them: number zero, empty array, empty string.
$tests = [0, [], "", false];
foreach ($tests as $condition)
{
if (!$condition)
{
echo "A condição é verdadeira!" . PHP_EOL;
}
}
Running the above test, you will notice that the four tests will pass, since the four values are considered false by PHP and, of course, negating it with !
, the test becomes true. However, when doing:
$tests = [0, [], "", false];
foreach ($tests as $condition)
{
if ($condition === false)
{
echo "A condição é verdadeira!" . PHP_EOL;
}
}
Only the last test will pass (remembering that the ===
operator checks if the parameters are identical, while ==
checks the equality of values).
Using !(condition)
is the same as condition == false
, but completely different from condition === false
.
For the filter_var
function this is important because by reading the documentation, you will see:
Valor retornado: Retorna o dado filtrado, ou FALSE se o filtro falhar.
If somehow the filtered value is considered false by PHP, even if it is valid, its !(condition)
condition will indicate the value as invalid while condition === false
will only indicate as invalid if the filter actually fails.
/ p>