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:27:43 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 03:12:34 von thyb0

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.

Well, array_walk() lets you use a callback function to treat your data,
which is a totally different approach to the structure of your script;
whereas yes, it's always possible (I guess) to do things with 'classic'
loops.

I don't pretend, it remains a good question; but IMO there isn't any
deeper reasoning behind it.

Anyway, concerning your doubts about what to use and when, I recently
found some really good reading here:
http://www.blueshoes.org/phpBench.php

I don't know if it answers your question correctly, but you should take
a look, it's worth it ;).

-thibĀ“

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

am 08.01.2008 03:26:12 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.
>

Please cross-post. Don't multipost. It wastes everyone's time.

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

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

am 08.01.2008 04:36:29 von Steve

"Jerry Stuckle" wrote in message
news:dJKdnVPbW53kQh_anZ2dnUVZ_jmdnZ2d@comcast.com...
> 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.
>>
>
> Please cross-post. Don't multipost. It wastes everyone's time.

just ignore the post, jerry. you like wasting everyone's time with other
things. hint: the pot and the kettle are both black.

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

am 08.01.2008 05:06:10 von Steve

wrote in message
news:4c7dc500-439b-4678-b14c-53db37c2f072@1g2000hsl.googlegr oups.com...
>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.

i've used both methods of to do the same things. it's really
6-1-0.5-the-other. i'd think array_walk would be most effective when used in
abstract instances/circumstances...say inside a class that generates html
but knows nothing about what it is supposed to create. i'll give you a 'for
instance' in a minute.

for me though, i prefer foreach as most code reads procedurally to the
programmer and doesn't require him to look around to find a callback
function. it's simply easier to follow what's going on in the script...not
to mention debug.

here's a typical scenario. you have to build a select list...selecting a
default or user-selected initial value for the list...array_walk makes very
short work of it:

function buildOptionList($display, $value, &$options)
{
$options[1][] = '';
}
$categories['Select Value 1'] = 'some value A';
$categories['Select Value 2'] = 'some value B';
$categories['Select Value 3'] = 'some value C';
$options = array();
$optionList = array($_REQUEST['selectInput'], &$options);
array_walk($categories, 'buildOptionList', $optionList);
?>






as you can see, you have a reusable callback that works for building any
select...in about four or 5 lines of code and everything is loosely coupled.
i've taken this approach. i've also used the same methodology to define the
form fields used for submission and getting and validating everything. i've
taken those results and generated insert/update/delete statements
dynamically using the same thing. essentially, what i had at the end was a
php template. i could certainly better separate the php from the html in
these scripts, however, they're harder to maintain than their foreach'ed
counter-parts...even though there is a bit less code.

anyway, just going on at the mouth. functionally, i don't see an advantage
to either one. from a managability standpoint, i tend to go with foreach.
your preferences may differ. just realize that the choice of which to use is
usually just that...a choice.

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

am 08.01.2008 10:22:42 von Betikci Boris

On Jan 8, 3:27 am, adam.timberl...@gmail.com wrote:
> I was reading this article yesterday:http://www.talkphp.com/advanced-php-programming/18 86-how-would-i-appl...
>
> 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.

When word comes to user defined functions, bundled functions seems
unnecessary ;) However default functions of PHP help to keep your code
clean, optimized and readable, etc.

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

am 08.01.2008 15:04:13 von Steve

"Betikci Boris" wrote in message
news:c9a8b6bc-d794-412d-86bf-eaa8c3b93698@x69g2000hsx.google groups.com...
> On Jan 8, 3:27 am, adam.timberl...@gmail.com wrote:
>> I was reading this article
>> yesterday:http://www.talkphp.com/advanced-php-programming/18 86-how-would-i-appl...
>>
>> 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.
>
> When word comes to user defined functions, bundled functions seems
> unnecessary ;) However default functions of PHP help to keep your code
> clean, optimized and readable, etc.

what?!!!???