I'd like to ask if it's possible to print "null" instead of nothingness in php8. Let me explain:
$player = null;
echo $player;
What it prints:
What I want: null
CodePudding user response:
Depending on what "nothingness" means to you, you could use one of the following:
echo $player ?? 'null'; // null coalescing operator
// or
echo $player ? $player : 'null'; // ternary operator
// or
echo !empty($player) ? $player : 'null'; // ternary operator with empty() check, which will not throw an error when the $player variable does not exist
// or
echo $player ?: 'null'; // ternary operator shorthand
More info here: PHP ternary operator vs null coalescing operator
CodePudding user response:
Edit
I came to the idea to use the new "null coalescing operator" which was added in php 7.0. You can find more details on [this][1] comment.echo $player ?? 'null';