Checking if isset and is true?
The empty()
function will do the job.
Use it with the not operator (!
) to test "if not empty", i.e.
if(!empty($var)){
}
You may use the ?? operator as such:
if($var ?? false){
...
}
What this does is checks if $var
is set and keep it's value. If not, the expression evaluates as the second parameter, in this case false
but could be use in other ways like:
// $a is not set
$b = 16;
echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16
More info here: https://lornajane.net/posts/2015/new-in-php-7-null-coalesce-operator