Is there a ternary operator or the like in PHP that acts like
?? of C#?
?? in C# is clean and shorter, but in PHP you have to do something like:
// This is absolutely okay except that $_REQUEST['test'] is kind of redundant. echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi'; // This is perfect! Shorter and cleaner, but only in this situation. echo null? : 'replacement if empty'; // This line gives error when $_REQUEST['test'] is NOT set. echo $_REQUEST['test']?: 'hi';
PHP 7 adds the null coalescing operator:
You could also look at short way of writing PHP's ternary operator ?: (PHP >=5.3 only)
And your comparison to C# is not fair. "in PHP you have to do something like" - In C# you will also have a runtime error if you try to access a non-existent array/dictionary item.