On Tuesday 19 April 2005 17:03, Jochem Maas wrote:
> Richard Lynch wrote:
> > On Mon, April 18, 2005 4:34 am, Sebastian said:
> >>$string = '4:gaming,5:hardware,3:software,8:security';
> >
> > $idcats = explode(',', $string);
> > while (list(, $idcat) = each($idcats)){
> >   list($id, $cat) = explode(':', $idcat);
> >   echo "\$id = $id<br />\n";
> >   echo "\$cat = $cat<br />\n";
> > }
>
> The 'other' guy mentioned that while() is faster than foreach,
> is this true?

http://www.sitepoint.com/article/php5-standard-library

"Note that the crude benchmarks I've performed suggest that calling the 
methods directly is faster than using foreach, because the latter introduces 
another layer of redirection that must be resolved at runtime by PHP."

>
> I read a few days ago somewhere on php.net that foreach() is the
> recommended (by php devs) way of iterating over arrays....
>
> 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
> 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()?
>
> not that I care too much, I find foreach() more pleasing to the eye and
> there is less to type (in the given example).
>
> :-)
>
> rgds,
> Jochem
>
> >>what is the best way to explode then loop this string after its taken
> >>apart.
> >>
> >>output should be something like:
> >>
> >>$id = 4
> >>$cat = gaming
> >>
> >>etc..
> >>
> >>im just looking for the best/fastest way to do this. the string can grow
> >>to
> >>200 or so bytes, maybe more.
> >
> > 200 bytes is chump-change.
> >
> > It really doesn't matter how you do this, within reason.

-- 

Cyberly yours,
Petar Nedyalkov
Devoted Orbitel Fan :-)

PGP ID: 7AE45436
PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc
PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436

Attachment: pgpFE7VCEyn7m.pgp
Description: PGP signature

Reply via email to