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?
Just my 20 cents -
null
initialises the variable with nothing. It prepares the variable to be garbage collected in the next cycle.It only does the following - puts it in the current scope so the interpreter is not considering it not existing. It's similar to what
global $global_var;
does.var_dump( isset( $null_var ) );
will outputbool(false)
anyway.Setting a variable to null is only useful for cases as with the one you circle around @everzet - having it as a function's default variable.