Why not use a foreach loop instead of having a function called

Why not use a foreach loop instead of having a function called

am 08.01.2008 02:28:07 von adam.timberlake

I was reading this article yesterday:
http://www.talkphp.com/advanced-php-programming/1886-how-wou ld-i-apply-htmlentities-every-array-item.html

I am wondering.. okay.. we can use array_walk but doesn't that just
loop through the items anyway ?? So why not just use the foreach loop
instead... is it a shorthand trick or is there any deeper reasoning
behind it... Thats my question!

I do have another question and that is if it is a shorthand version,
having a foreach loop isn't much more in terms of the letters you type
and so why have the PHP developers put it in there ??

It's puzzling me. Thank you in advance.

Re: Why not use a foreach loop instead of having a function called array_walk?

am 08.01.2008 02:56:25 von Jerry Stuckle

adam.timberlake@gmail.com wrote:
> I was reading this article yesterday:
> http://www.talkphp.com/advanced-php-programming/1886-how-wou ld-i-apply-htmlentities-every-array-item.html
>
> I am wondering.. okay.. we can use array_walk but doesn't that just
> loop through the items anyway ?? So why not just use the foreach loop
> instead... is it a shorthand trick or is there any deeper reasoning
> behind it... Thats my question!
>
> I do have another question and that is if it is a shorthand version,
> having a foreach loop isn't much more in terms of the letters you type
> and so why have the PHP developers put it in there ??
>
> It's puzzling me. Thank you in advance.
>

It's just another way of doing things. In programming there are often
multiple ways of doing things, i.e. (to add to the book example)

$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" =>
"apple");

function test_print($item2, $key)
{
echo "$key. $item2
\n";
}

// As a foreach loop:

foreach($array as $key->$value)
test_print($value, $key);

// Or, with array_walk)
array_walk($fruits, 'test_print');


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================