Reputation: 7318
It works fine and doesn't cause problem
echo $formErrorBag[ 'email' ] ?? null
But is it an accepted practice? Never saw an example of this used with null
.
Upvotes: 2
Views: 208
Reputation: 7911
The null coalesce operator checks values with isset()
, so this:
echo $formErrorBag['email'] ?? null;
Equals:
if(isset($formErrorBag['email'])){
echo $formErrorBag['email'];
} else {
echo null;
}
I really don't see the point in that as you are still executing a function doing literally nothing. If you are doing this to avoid raising an E_NOTICE
you can simply turn it off with error_reporting()
as doing your method kinda breaks the entire point of that.
It's there to warn you about a possible bug in your code, not finding techniques to suppress it.
error_reporting(error_reporting() ^ E_NOTICE); // turn notices off keep remaining flags intact.
echo $array['doesnotexist'];
echo $array['etc'];
error_reporting(error_reporting() | E_NOTICE); // turn it back on.
Upvotes: 1
Reputation: 311188
It's completely legal, and accepted. It's a pretty easy and elegant way to avoid raising an E_NOTICE
if $formErrorBag
doesn't have an 'email'
key.
Upvotes: 7