Before dealing with your question, I'd like to point out that you probably want to use empty()
instead of comparing with an empty string and that if you really want to compare to an empty string, use the ===
operator.
Anyway, default initialization is a real problem in many languages. C# even has a ?? operator to solve this kind of issue! So, let's try to make it better to make your code better step by step.
Ternary operator no more
The one-liner is too long, and using the ternary operator on multiple lines is a bad idea: that's what if-statement are for:
if (empty($newsItems[0]['image_url'])) {
$newsItems[0]['image_url'] = '/img/cat_placeholder.jpg';
}
This is more readable for two reasons : it's easier to understand the syntax, but more importantly, it's easier to see that you simply want a default value when there's nothing. We don't need the 'else' part which is confusing with the ternary operator.
Note: as mentioned by Simon Scarfe and corrected by mseancole, PHP also has some special ternary syntax to do this:
$newsItems[0]['image_url'] = $newsItems[0]['image_url'] ?: 'img/cat_placeholder.jpg';
Factorize it!
If you're doing this only once or twice, then all is good, but otherwise you'd want to factorize it into a function, since you don't repeat yourself, right? The simple way is:
function default_value($var, $default) {
return empty($var) ? $default : $var;
}
$newsItems[0]['image_url'] = default_value($newsItems[0]['image_url'], '/img/cat_placeholder.jpg');
(The ternary operator does make sense here since variable names are short and both branches of the condition are useful.)
Still too long
Can we do better? Yes we can! We're writing the variable name twice, but passing by reference can help us here:
function default_value(&$var, $default) {
if (empty($var)) {
$var = $default;
}
}
default_value($newsItems[0]['image_url'], '/img/cat_placeholder.jpg');
It's much shorter, and feels more declarative: you can look at a bunch of default_value
calls and see what the default values are instantly.