doubt in an if and symbol!

0

I have this if I explain it to a partner but I do not understand it well

if (!($result = $db->sql_query($q)));

Could you explain how it is read and how it works?  thanks

    
asked by Carlos Enrique Gil Gil 30.04.2018 в 19:28
source

1 answer

2

! is called the denial opeardor. This what it does is that it denies the Boolean expression according to the result of it. For example:

echo !true;

You'll notice how it prints false , this because false is the denial of true .

In your case, the function $db->sql_query($q) returns a boolean. That result is saved in $result so $result is now type boolean , if the result is true , this is denied to false so the if is not met and ignored your body, otherwise if you return false , this would be denied and become true so if you run the body of if .

This would be the broken code to be better understood:

$result = $db->sql_query($q);
if (!$result)
{
 //...
}

The if would only be executed if $ result were false, since! Deny the value false to true .

    
answered by 30.04.2018 / 19:47
source