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?
Best way is always the ternary operators. Sorry to open this up again, but I spent a good 20 min testing out cases because this conversation did so many loops through multiple techniques but didn't appear to settle.
The form:
and an example:
I settled on this because it appeared to be the most versatile. You can set a default value and if your using a false evaluating value as the default its easy to see if the variable was set. a.k.a. in this example if I do:
I know that the value is not set and if I want to check if it is set:
And it may be a bit verbose but you can do it all in one swoop like this:
And like has been said, you can shorthand the ternary and just use:
But you will get a notice thrown on array value not set so I always just use isset and assign a value in the ternary to avoid silencing messages for no reason.