Home Blog Super Global Variables in PhP

Super Global Variables in PhP

0
Super Global Variables in PhP

Several predefined variables in PHP are “superglobals”, which means that they are always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.

PHP super global variable is used to access global variables from anywhere in the PHP script. PHP Superglobal variables are accessible on the same page that defines it, as
well as outside the page. While local variable’s scope is within the page that defines it.

The PHP superglobal variables are:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

$GLOBALS: $GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

$_SERVER[“ConstantName”] : $_SERVER holds information about headers, paths, and script locations. eg. $_SERVER[“SERVER_PORT”], $_SERVER[“SERVER_NAME”], $_SERVER[“REQUEST_URI”]

$_REQUEST[“FormElementName”]: This can be used to collect data with both post and get method.

$_POST[“FormElementName”] : It is used to collect value in a form with method=”post”>. Information sent from a form is invisible to others.(can check on the address bar)

$_GET[“FormElementName”] : It is used to collect value from a form(HTML script) sent with method=’get’. Information sent from a form with the method=’get’ is visible to everyone(it display on the browser URL bar).

$_FILES[“FormElementName”]: It can be used to upload files from a client computer/system to a server.
OR
$_FILES[“FormElementName”][“ArrayIndex”]: Such as File Name, File Type, File Size, File temporary name.

$_ENV: $_ENV is a superglobal that contains environment variables. Environment variables are provided by the shell under which PHP is running, so they may vary according to different shells.

$_COOKIE[“VariableName”]: A cookie is used to identify a user. a cookie is a small file that the server embedded on a user computer.

$_SESSION[“VariableName”]: A session variable is used to store information about a single user, and are available to all pages within one application.

LEAVE A REPLY

Please enter your comment!
Please enter your name here