From 887bcfd33f41f19689b026e6f115689d7c5e9ce0 Mon Sep 17 00:00:00 2001 From: Pieter Hordijk Date: Fri, 13 Jul 2012 18:49:23 +0000 Subject: [PATCH] Improved for() docs git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@326628 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/control-structures/for.xml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/language/control-structures/for.xml b/language/control-structures/for.xml index 4eea2ba400..1ccec7d4be 100644 --- a/language/control-structures/for.xml +++ b/language/control-structures/for.xml @@ -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) - 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: @@ -148,11 +148,11 @@ for($i = 0; $i < sizeof($people); ++$i) '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); }