|
Posted by Jochem Maas on 04/19/05 21:44
Petar Nedyalkov wrote:
> 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?
>
sorry to call you the 'other' guy, Petar - I was being lazy.
>
> 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."
are we talking about iterating over an Iterator or an array()?
Harry Fuecks is talking about iterating over a php5 object..., your
question/example features a straight array.
>
>
>>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.
>
>
[Back to original message]
|