You are here: Re: Bind variables to a simple array « PHP Programming Language « IT news, forums, messages
Re: Bind variables to a simple array

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация