Skip to content

Instantly share code, notes, and snippets.

@anytizer
Last active May 16, 2016 23:06
Show Gist options
  • Save anytizer/f3c9abd7c3325a0b8614711f022e990f to your computer and use it in GitHub Desktop.
Save anytizer/f3c9abd7c3325a0b8614711f022e990f to your computer and use it in GitHub Desktop.
Global Variable Scopes
<?php
header('Content-Type: text/plain');
# Normal server environment
ini_set('display_errors', 'Off');
error_reporting(0);
$test = 'TEST VALUE';
function test()
{
# Expect error here
echo "\$test: ", $test, "\r\n";
# Ok
echo "\$GLOBALS['test']: ", $GLOBALS['test'], "\r\n";
# Ok
global $test;
echo "\$test after `global \$test`: ", $test, "\r\n";
}
test();
/**
$test:
$GLOBALS['test']: TEST VALUE
$test after `global $test`: TEST VALUE
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment