note 94657 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Tue, 17 Nov 2009 15:26:24 +0000 |
| Subject: | note 94657 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162986@lists.php.net to get a copy of this message | ||
Note Submitter: Stewart
----
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>";
?>