note 94657 added to language.variables.scope

From: Date: Tue, 17 Nov 2009 10:26:21 +0000
Subject: note 94657 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-162971@lists.php.net to get a copy of this message
moraesdno - your result seems to be due to your using each global variable only once in each function. The longer time of the first function appears to be due to there being two stages: importing the global variable and actually using it. But if you use global, the importation happens only once per call to the function. OTOH, if you use $GLOBALS, it is looked up again each time. <?php //Using the keyword global function sum() { global $a, $b; $a=1; $b=2; while ($a < 1000) { $a += $b; $b += $a; } } $t = microtime(true); for($i=0; $i<1000; $i++) { sum(); } echo microtime(true)-$t; echo " -- ".$a."<br>"; //Using the superglobal array function sum2() { $GLOBALS['a'] = 1; $GLOBALS['b'] = 2; while ($GLOBALS['a'] < 1000) { $GLOBALS['a'] += $GLOBALS['b']; $GLOBALS['b'] += $GLOBALS['a']; } } $t = microtime(true); for($i=0; $i<1000; $i++) { sum2(); } echo microtime(true)-$t; echo " -- ".$a."<br>"; ?> ---- Server IP: 92.48.74.199 Probable Submitter: 87.194.178.55 ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- https://master.php.net/note/edit/94657 Del: integrated -- https://master.php.net/note/delete/94657/integrated Del: useless -- https://master.php.net/note/delete/94657/useless Del: bad code -- https://master.php.net/note/delete/94657/bad+code Del: spam -- https://master.php.net/note/delete/94657/spam Del: non-english -- https://master.php.net/note/delete/94657/non-english Del: in docs -- https://master.php.net/note/delete/94657/in+docs Del: other reasons-- https://master.php.net/note/delete/94657 Reject -- https://master.php.net/note/reject/94657 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162971) next »