Using the Null Coalescing Operator with Functions in PHP | Easy examples

When calling a function that may return null, we can use the Null Coalescing operator (??) to provide a default value.

Using Null Coalescing operator with a function.

(click on the image to open in a new tab)

We can even use a function’s return value as the default value.

Example of providing a second function as default.

(click on the image to open in a new tab)

In case the left function is nullish, we can have the other one’s returned value as the default.

If the first function returns a nullish value, the second one is used as default.
(click on the image to open in a new tab)

Although my blog doesn’t support comments, feel free to reply via email or X.