In PHP, sometimes you'll need to do alot of variable assignment like
[ Hide ] <?php
// simple
$bar = isset($foo) ? $foo : '';
// complicated
$bar = isset($foo[$test['index']]) ? $foo[$test['index']] : '';
?>
So I came up with this function to ease my life...
[ Hide ] <?php
// Use this to standardize things..
function isset_empty(&$input)
{
return isset($input) ? $input : '';
}
?>
Things works fine at first. But later then I discovered that it's not working like what I expected when I pass a value inside an array to that function. See below:
[ Hide ] <?php
$bar = isset_empty($foo); // $foo is undefined at this point
echo $bar;
$foo = array(1 => 'World', 2 => 'Cup');
print_r($foo);
$bar = isset_empty($foo[10]);
print_r($foo);
// The outpust result
// <-- emptry string, expected
// Array ( [1] => World [2] => Cup )
// Array ( [1] => World [2] => Cup [10] => )
?>
It seems that index 10 with empty string is inserted to the
$foo array after calling that function. It doesn't harm in some situation but it's a real disaster if you're using array to populate items or doing a
count($foo) to check the array size. The results are affected !
So I have to replace all the statements that uses the function and go back to the old way
$bar = isset($foo[$test['index']]) ?$foo[$test['index']] : '';
Doh!