You are here: Re: [PHP] explode a string « PHP « IT news, forums, messages
Re: [PHP] explode a string

Posted by Jochem Maas on 04/19/05 17:03

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?

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.
>

 

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

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