But half of my programming time I'm working with the server-side mostly in PHP and sometimes there are situations where I would love to have this handy functions in my toolbox.
So here comes one of them the $A functions which acts the quite the same as in mootools and has one thing extra, the 'force' option.
It checks if the variable is an array if yes than it returns the unchanged variable if not it wraps the variable in a new array. The force option bypass this test and wraps the variable in a new array even if it is already an array. That's all, simple but powerful.
function A($var, $force=false){
return ($force || !is_array($var) ? array($var) : $var);
}
Usage:
It helped me a lot and maybe it can help you too.
//Without forcing
$var1 = 'hello world';
$var1 = A($var1);
//$var1 is wrapped => array('hello world')
$fruits = array('apple', 'banana', 'cherry');
$fruits = A($fruits);
//$fruits is unchanged => array('apple', 'banana', 'cherry')
//With forcing
$fruits = A($fruits, true);
//$fruits is wrapped => array(array('apple', 'banana', 'cherry'))
0 comments:
Post a Comment