Improved for() docs

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@326628 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Pieter Hordijk 2012-07-13 18:49:23 +00:00
parent 6e3a76bb60
commit 887bcfd33f

View file

@ -122,11 +122,11 @@ endfor;
* when running through the for loop.
*/
$people = Array(
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0; $i < sizeof($people); ++$i)
for($i = 0; $i < count($people); ++$i)
{
$people[$i]['salt'] = rand(000000, 999999);
}
@ -136,11 +136,11 @@ for($i = 0; $i < sizeof($people); ++$i)
</informalexample>
</para>
<simpara>
The problem lies in the second for expression. This code can be slow
The problem lies in the second for expression. This code can be slow
because it has to calculate the size of the array on each iteration.
Since the size never change, it can be optimized easily using an
intermediate variable to store the size and use in the loop instead
of sizeof. The example below illustrates this:
Since the size never changes, it can be optimized easily using an
intermediate variable to store the size and use in the loop instead
of count. The example below illustrates this:
</simpara>
<para>
<informalexample>
@ -148,11 +148,11 @@ for($i = 0; $i < sizeof($people); ++$i)
<![CDATA[
<?php
$people = Array(
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0, $size = sizeof($people); $i < $size; ++$i)
for($i = 0, $size = count($people); $i < $size; ++$i)
{
$people[$i]['salt'] = rand(000000, 999999);
}