When building applications, performing comparisons is very common. Recent PHP versions improved the good old "if-else" with a few shorter syntax options.
Ternary Operator: "? ... :"
The ternary operator is used to shorten the if/else.
Instead of writing this:
if (request()->has('customer_id')) { return request()->get('customer_id');} else { return null;}
You can write this:
request()->has('customer_id') ? request()->get('customer_id') : null
This ternary operator can be shortened using the "Elvis" "?:" operator.
request()->get('customer_id') ?: null
In this case, the returned value will be from the request or null
.
Null coalescing: "??"
The null coalescing operator has been...