I have been working with PHP for a long time, but for me it was always a mystery, the correct use of an exclamation mark (negative sign) in front of variables.
What does !$var indicate? Is var false , empty, not set, etc.?
Here are some examples I need to learn ...
Example 1:
$string = 'hello'; $hello = (!empty($string)) ? $string : ''; if (!$hello) { die('Variable hello is empty'); }
Is this example valid? Would the if statement really work if $string was empty?
Example 2:
$int = 5; $count = (!empty($int)) ? $int : 0; // Note the positive check here if ($count) { die('Variable count was not empty'); }
Will this example be valid?
I never use any of the above examples, I limit these if ($var) variables that have only boolean values. I just need to know if these examples are valid, so I can extend the use of if ($var) . They look very clean.
Thanks.
source share