note 96879 added to language.variables.scope
| From: | rybadour at gmail dot com | Date: | Sat, 20 Mar 2010 20:51:52 +0000 |
| Subject: | note 96879 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167255@lists.php.net to get a copy of this message | ||
Response to moraesdno:
Using the superglobal $GLOBALS isn't always faster. In fact, in most situations its slower. And
it gets worse the more times you need to use the same variable:
<?php //============================
$a=1;
function sum() {
global $a;
$a += 1;
$a += 1;
}
$a=1;
function sum2() {
$a = $GLOBALS['a'];
$a += 1;
$a += 1;
$GLOBALS['a'] = $a;
}
?>
Gives a time of:
0.00039410591125488 <-- global $a;
0.00063490867614746 <-- $GLOBALS['a'];
<?php //============================
$a=1;
function sum() {
global $a;
$a += 1;
$a += 1;
}
$a=1;
function sum2() {
$GLOBALS['a'] += 1;
$GLOBALS['a'] += 1;
}
?>
Gives a time of:
0.0003960132598877 <-- global $a;
0.0005040168762207 <-- $GLOBALS['a'];
<?php //============================
$a=1;
$b=2;
function sum() {
global $a, $b;
$a += $b;
$a -= $b;
}
$a=1;
$b=2;
function sum2() {
$GLOBALS['a'] += $GLOBALS['b'];
$GLOBALS['a'] -= $GLOBALS['b'];
}
?>
Gives a time of:
0.0005180835723877 <-- global $a;
0.00077199935913086 <-- $GLOBALS['a'];
<?php //============================
$a=1;
$b=2;
function sum() {
global $a, $b;
$a += $b;
$a -= $b;
}
$a=1;
$b=2;
function sum2() {
$a = $GLOBALS['a']
$b = $GLOBALS['b'];
$a += $b;
$a -= $b;
$GLOBALS['a'] = $a;
}
?>
Gives a time of:
0.00052213668823242 <-- global $a;
0.0013630390167236 <-- $GLOBALS['a'];
----
Server IP: 69.147.83.197
Probable Submitter: 24.57.222.121
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/96879
Del: integrated -- https://master.php.net/note/delete/96879/integrated
Del: useless -- https://master.php.net/note/delete/96879/useless
Del: bad code -- https://master.php.net/note/delete/96879/bad+code
Del: spam -- https://master.php.net/note/delete/96879/spam
Del: non-english -- https://master.php.net/note/delete/96879/non-english
Del: in docs -- https://master.php.net/note/delete/96879/in+docs
Del: other reasons-- https://master.php.net/note/delete/96879
Reject -- https://master.php.net/note/reject/96879
Search -- https://master.php.net/manage/user-notes.php