programmieren:php:nullsafe
Nullsafe
Nullsafe in PHP basically means that incase any given value somehow ends up as null, it is correctly handled, may that be an expection, a validation error etc.
Theres another way. In PHP theres a “Nullsafe operator” written as “??” which basically replaces the given value with a set value, incase the given value is null.
For example, a value must be a string, but empty strings are allowed:
$george = null; $stringthingy->theParameterToThisFunctionMustBeAString($george ?? '');
This doesn't fail even though george is null. the ?? '' replaces it with an empty string which is allowed in that function.
This can be used strategically to correctly map null values to their needed value, whether it be 0, '', [] or false.
Or maybe any other default value.
programmieren/php/nullsafe.txt · Last modified: by admin
