|
Posted by Janwillem Borleffs on 11/15/07 11:23
Andreas Thiele wrote:
> function test($arr) {
> // create bindings for $foo, $bar and $baz by evaluating "$foo =
> $arr[0]; ..."
> eval(abind("arr", array("foo", "bar", "baz")));
The eval is useless here. In general, do not use eval because it's quite
expensive and in 9.99 out of 10 cases you can do without.
> return $foo."-".$bar."-".$baz;
> }
>
> echo test(array(1, 2, 3));
>
The way the test function works now, all you really need is:
function test($arr) {
return implode('-', $arr);
}
> Of course function test will use $foo, $bar and $baz really oftern. I
> could as well use $arr[0] and so on, but I'd like to use more
> describing variable names. In this case the eval line would make
> things more succint and thus improve code readability.
>
When test was assigning global vars it would make sense to use specific
variable names to prevent overwriting existing ones (although you need to
perform additional tests in that case). In the current context, test only
creates local variables which are available in the function's namespace only
and the test function I have suggestion will do.
JW
Navigation:
[Reply to this message]
|