Skip to content

Instantly share code, notes, and snippets.

@houssemz
Last active June 20, 2018 20:35
Show Gist options
  • Save houssemz/291410b0cb7bcdc50cbaeed1b7c0cc55 to your computer and use it in GitHub Desktop.
Save houssemz/291410b0cb7bcdc50cbaeed1b7c0cc55 to your computer and use it in GitHub Desktop.

GLOBALS :

An associative array containg references to all variables defined in the global scope of the script. The variable names are the keys of the array

$_SERVER :

Is an array that containing inforamtion such as headers, paths and script locations.

Prior to PHP 5.4.0, $HTTP_SERVER_VARS contained the same initial information, but was not a superglobal. (Note that $HTTP_SERVER_VARS and $_SERVER were different variables and that PHP handled them as such.)

⛔ You may or may not find any of the following elements in $_SERVER if running PHP on the command line.

$argv :

Contains an array of all the arguments passed to the script when running from the command line.
The first argument $argv[0] is always the name that was used to run the script.

$argc :

Return the number of arguments returned passed to the current script => the minumu value of $argc is 1

'SERVER_PROTOCOL'

Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0';

'REQUEST_METHOD'

Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.

'DOCUMENT_ROOT'

The document root directory under which the current script is executing, as defined in the server's configuration file.

'HTTP_ACCEPT'

Contents of the Accept: header from the current request, if there is one.

'HTTP_ACCEPT_CHARSET'

Contents of the Accept-Charset: header from the current request, if there is one. Example: 'iso-8859-1,*,utf-8'.

'HTTP_ACCEPT_ENCODING'

Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'.

'REMOTE_ADDR'

The IP address from which the user is viewing the current page.

'SCRIPT_FILENAME'

The absolute pathname of the currently executing script.
@houssemz
Copy link
Author

houssemz commented Jun 20, 2018

⛔ NB :

  • All elements of the $_SERVER array whose keys begin with 'HTTP_' come from HTTP request headers and are not to be trusted.
  • All HTTP headers sent to the script are made available through the $_SERVER array, with names prefixed by 'HTTP_'.

@houssemz
Copy link
Author

houssemz commented Jun 20, 2018

     <?php
       function test()
       {
           $foo = "local variable";

           echo '$foo in global scope: ' . $GLOBALS["foo"], PHP_EOL;
           echo '$foo in current scope: ' . $foo, PHP_EOL;
       }

     $foo = "Example content";
     test();
 # output
 $foo in global scope: Example content
 $foo in current scope: local variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment