You know how, in JavaScript, we can set a value to a variable if one doesn't, like this:
name = name || 'joe';
This is quite common and very helpful. Another option is to do:
name || (name = 'joe');
Well, in PHP, that doesn't work. What many do is:
if ( empty($name) ) $name = 'joe';
Which works...but it's a bit verbose. My preference, at least for checking for empty strings, is:
$name = $name ?: 'joe';
What's your preference for setting values if they don't already exist?
@JeffreyWay I like the last option as long as the var has a default set like in your example function to avoid the undefined variable message. Or shorten it to one line by doing it this way.
return $name ? $name : 'joe';
If the variable isn't set to '' by default it doesn't seem like there would be a way around using isset() to avoid the warning.