Can someone explain to me why this strange thing is happening? I have an empty $_POST array and i want to use a variable in that array that might be defined or not (in that case - use some default value). To do so, i have a function to avoid using all those isset() or empty() checks:
function val(&$varToCheck, $defaultValue = false)
{
if (isset($varToCheck))
return $varToCheck;
return $defaultValue;
}
Now if i say:
val($_POST['test']); print_r($_POST['test']);
The $_POST array now contains a NULL value key "test". I assume this is happening because the variable is passed by reference and somehow auto-creates an array index. How could i avoid this behaviour?
val(&$_POST['test']); print_r($_POST['test']);, and remove&sign from function declaration