Skip to content

Instantly share code, notes, and snippets.

@kchristensen
Last active December 12, 2015 07:49
Show Gist options
  • Save kchristensen/eb4bd6aa3200dafbe52d to your computer and use it in GitHub Desktop.
Save kchristensen/eb4bd6aa3200dafbe52d to your computer and use it in GitHub Desktop.
PHP Globals micro optimization
/*
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