Possible problems with logical operators

Asked

Viewed 45 times

0

Hello, I wonder if you have any problem or will generate future failures, use the operator "!" this way:

if (!(filter_var($email, FILTER_VALIDATE_EMAIL))):
echo "<script>alert('O email digitado: ".$email. " não é válido!');</script>";
echo "<script>window.history.back();</script>"; 
exit;
else:

instead of the way it is on the W3C website:

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
   echo("$email is a valid email address");
} else {
    echo("$email is not a valid email address");
}

W3C - In this case I only refer to the use of the operator "!" in the first line and would like to know also along with the "=== false" if you have problems this difference or not.

  • 1

    I tested and there is no difference, the two work well, tested with zero, false and null. As for the future only God knows :)

2 answers

3


There is the difference, but I don’t know if it exactly applies to the case presented. The difference between using !(condition) and condition === false is that PHP naturally considers some values, different from false, as false. Some of them: zero number, 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, as the four values are considered false by PHP and obviously negating it with !, the test becomes true. However, in 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 whether the parameters are identical, while the == notes the equality of values).

Utilise !(condition) is the same as doing condition == false, but completely different from condition === false.

For the function filter_var this is important because when 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.

0

The operator ! is already basically a === false.

He does the operation for example:

if(!isset($varx)){...

would mean:

If the $varx is not arrow...

Or:

If the $varx is exactly the same the fake...

They have the same effect, not needing to use both together.

And in fact, in the example he’s reversing the answer, saying that if the wrong email filter is fake he’s right (got confused but that’s what he’s doing)

  • 3

    Actually do !(condition) is the same as condition == false. Use the operator === completely changes the result.

  • I asked why it was better than getting the doubt, go that there is some big dilemma behind a "!" but it is good to know

Browser other questions tagged

You are not signed in. Login or sign up in order to post.