Skip to content

Instantly share code, notes, and snippets.

@Hexodus
Last active April 23, 2017 21:02
Show Gist options
  • Save Hexodus/c8c92fc03720f9f215de1ad0841196f7 to your computer and use it in GitHub Desktop.
Save Hexodus/c8c92fc03720f9f215de1ad0841196f7 to your computer and use it in GitHub Desktop.
$GLOBALS vs define() vs const; It depends. I would go for `define()` because it has a more compact syntax in usage. But `define()` can only hold scalar values in PHP < 7.0 In case you need i.e. an associative array you have no other choice then to go for ` $GLOBALS` or use PHP >=7.0. From http://stackoverflow.com/questions/10691404/php-best-way-…
// Storing a single value works fine with define
define( 'ROOT_DIR', dirname(dirname(__FILE__)) . '/' );
// But not for complex data types like this array
$USERPIC_PARAMS = array(
"user_root" => "images/users",
"padding_length" => 8,
"split_length" => 4,
"hash_length" => 12,
"hide_leftover" => false
);
// Then you need $GLOBALS
$GLOBALS['USERPIC_PARAMS'] = $USERPIC_PARAMS;
// Or in PHP >=7.0
define( 'USERPIC_PARAMS', $USERPIC_PARAMS );
// output your define
echo ROOT_DIR;
// output your $GLOBALS var
echo $GLOBALS['USERPIC_PARAMS'];
// output in PHP >=7.0 your constant
echo USERPIC_PARAMS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment