Last active
December 12, 2015 07:49
-
-
Save kchristensen/eb4bd6aa3200dafbe52d to your computer and use it in GitHub Desktop.
PHP Globals micro optimization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
foreach ($GLOBALS as $field => $value) | |
{ | |
if (!in_array($field, array('_ENV', '_POST', '_GET', '_SERVER', '_FILES', '_REQUEST', 'GLOBALS', '_COOKIE'))) | |
{ | |
$data[$field] = $value; | |
} | |
} | |
*/ | |
/* | |
* The following code is ~30% faster than the above (and doesn't call in_array() 140+ times) | |
* | |
* -Kyle | |
*/ | |
foreach (preg_grep('/^(_(COOKIE|ENV|FILES|GET|POST|REQUEST|SERVER)|GLOBALS)$/', array_keys($GLOBALS), PREG_GREP_INVERT) as $field => $value) | |
{ | |
$data[$value] = $GLOBALS[$value]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment