explode a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry jocham, for you getting this twice.

I'd assume foreach is recommended because it lends to more readable
code. More readable code, is generally considered better code.

Personally I'd disagree and use while( list() = each() ), because it
doesn't create a copy of the array in memory, especially for the
array. If you're dealing with an array where the elements are large,
or multi-dimentional, you may want to consider a different approach,
such as assigning by reference, like:

for($keys = array_keys($sample_array); $key = each($keys), $val =&
$sample_array[$key]; )

but that is very ugly untested code(and I'm drunk), so I wouldn't
recommend you copy n paste it.

besides for the sake of 200 bytes, with an average of around 5
characters per word, it pretty much doesn't matter, which you use.
Even something as crappy as ASP would easly deal with 200b.

On 4/20/05, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote:
> Richard Lynch wrote:
> > On Tue, April 19, 2005 7:03 am, Jochem Maas said:
> >
> >>The 'other' guy mentioned that while() is faster than foreach,
> >>is this true?
> >
> >
> > Don't know ; Don't care.
> >
> > You should never loop through so many things in PHP that it matters in the
> > first place :-)
> >
> >
> >>I read a few days ago somewhere on php.net that foreach() is the
> >>recommended (by php devs) way of iterating over arrays....
> >
> >
> > [shrug]
> >
> > That's probably because they're tired of people not understanding the
> > internal pointer array, and asking FAQs about it.  Or maybe not.  Ask them
> > why they prefer it.  I sure don't know.
> >
> >
> >>also, compare these 2 lines:
> >>
> >>while (list(, $idcat) = each($idcats)){ /* ... */ }
> >>foreach ($idcats as $idcat){ /* ... */ }
> >>
> >>now its seems to me that the foreach version is 'up' 2 function calls
> >
> >
> > None of those are function calls.
> >
> > They are all language constructs.  Okay, each() *might* be a function...
> >
> > I'm not sure how much difference there is in the number of language
> > constructs used, nor if they are even comparable in sheer numbers the way
> > functions are.
>
> ah yes, lang constructs rather than function calls.
>
> >
> > foreach is probably slower, I think, because it creates a *copy* of the
> > array to work on, so it won't mess up the original and its internal
> > pointer.
>
> unless I'm mistaken its a copy-on-change, so unless you are changing the
> the array inside the loop you don't suffer the actuall copy penalty - can anyone
> knowledgable on php internals confirm or deny this?
>
> actually now I think of it you can use references in a foreach statement:
>
> php -r '
> $arr = array(1,2,3);
> foreach($arr as $k => &$v) {
>         $v++;
> }
> var_dump($arr);
> '
>
> which suggests that a copy is not (always?) being made...
>
> >
> > Again, with 200 bytes, you are wasting your time to worry about any of this.
>
> true, It's purely a theoretical interest - deeper understanding is alway nice :-)
> ...its not even my 200 bytes we're talking about ;-)
>
> >
> >
> >>on the while loop, all else being equal the foreach loop has to be faster
> >>(given that calling functions is relatively very expensive)...
> >>or is foreach() _really_ heavy when compared to while()?
> >
> >
> > Why don't you just benchmark it on your own machine and find out?
>
> because I don't have the skills to write a test/benchmark that I _know_ is
> kosher (and not skewed by a million of my misconceptions, besides I run so much
> stuff on my machine that speed can be severely affected by things like
> apache or firebird running in the background....
>
> that and I lazy ;-) (or I just don't care enough to invest time investigating this)
>
> >
> >
> >>not that I care too much, I find foreach() more pleasing to the eye and
> >>there is
> >>less to type (in the given example).
> >
> >
> > I'm an old dog, and I don't quite understand for sure how this new-fangled
> > foreach thingie works.  I'd spend more time looking it up and reading
> > about it than just typing what I *know* works. [shrug]
>
> >
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux