Industrial Training




PHP Global Variables - Superglobals


Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available in all scopes.


PHP Global Variables - Superglobals


Some 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.


The PHP superglobal variables are:


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

PHP Superglobal - $GLOBALS


Super global variables are built-in variables that are always available in all scopes.


PHP $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.
The example below shows how to use the super global variable $GLOBALS:


Example
< ?php 
$x = class="phpnumbercolor" style="color:red">75; 
$y = class="phpnumbercolor" style="color:red">25;
 
class="phpkeywordcolor" style="color:mediumblue">function addition() { 
  class="phpglobalcolor" style="color:goldenrod">$GLOBALS[class="phpstringcolor" style="color:brown">'z'] = class="phpglobalcolor" style="color:goldenrod">
  $GLOBALS[class="phpstringcolor" style="color:brown">'x'] + class="phpglobalcolor" style="color:goldenrod">$GLOBALS[class="phpstringcolor" style="color:brown">'y']; 
}
 
addition(); 
class="phpkeywordcolor" style="color:mediumblue">echo $z; 
class="phptagcolor" style="color:red">?>

In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function!


PHP Superglobal - $_SERVER


Super global variables are built-in variables that are always available in all scopes.


PHP $_SERVER


$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.
The example below shows how to use some of the elements in $_SERVER:


Example
< ?php 
class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" style="color:brown">'PHP_SELF'];
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"< br>";
class="phpnumbercolor" style="color:red"> class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">
$_SERVER[class="phpstringcolor" style="color:brown">'SERVER_NAME'];
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"< br>";
class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" style="color:brown">'HTTP_HOST'];
class="phpnumbercolor" style="color:red"> class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"< br>";
class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" style="color:brown">'HTTP_REFERER'];
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"< br>";
class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" style="color:brown">'HTTP_USER_AGENT'];
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"< br>";
class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" style="color:brown">'SCRIPT_NAME'];
class="phptagcolor" style="color:red">?>

In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function!


PHP Superglobal - $_REQUEST


Super global variables are built-in variables that are always available in all scopes.


PHP $_REQUEST


PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form.
The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the < form> tag. In this example, we point to this file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:


Example
< html class="tagcolor" style="color:mediumblue">>
< body class="tagcolor" style="color:mediumblue">>

< form class="attributecolor" style="color:red"> method="attributevaluecolor" style="color:mediumblue">="post" action="attributevaluecolor" style="color:mediumblue">="="color:black">
="phptagcolor" style="color:red">< ?php class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" 
style="color:brown">'PHP_SELF'];class="phptagcolor" style="color:red">?>"="tagcolor" style="color:mediumblue">>
  Name: < input class="attributecolor" style="color:red"> type="attributevaluecolor" style="color:mediumblue">="text" name="attributevaluecolor" style="color:mediumblue">
  ="fname"="tagcolor" style="color:mediumblue">>
  < input class="attributecolor" style="color:red"> type="attributevaluecolor" style="color:mediumblue">="submit"="tagcolor" style="color:mediumblue">>
< /form class="tagcolor" style="color:mediumblue">>

< ?php
class="phpkeywordcolor" style="color:mediumblue">if (class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" style="color:brown">"REQUEST_METHOD"] 
== class="phpstringcolor" style="color:brown">"POST") {
  class="commentcolor" style="color:green">// collect value of input field
  $name = class="phpglobalcolor" style="color:goldenrod">$_REQUEST[class="phpstringcolor" style="color:brown">'fname'];
class="phpnumbercolor" style="color:red">   class="phpkeywordcolor" style="color:mediumblue">if (class="phpkeywordcolor" style="color:mediumblue">empty($name)) {
    class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"Name is empty";
  } class="phpkeywordcolor" style="color:mediumblue">else {
class="phpnumbercolor" style="color:red">     class="phpkeywordcolor" style="color:mediumblue">echo $name;
  }
}
class="phptagcolor" style="color:red">?>

< /body class="tagcolor" style="color:mediumblue">>
< /html class="tagcolor" style="color:mediumblue">>

In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function!


PHP Superglobal - $GLOBALS


Super global variables are built-in variables that are always available in all scopes.


PHP $_POST


PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the < form> tag. In this example, we point to the file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_POST to collect the value of the input field:


Example
< html class="tagcolor" style="color:mediumblue">>
< body class="tagcolor" style="color:mediumblue">>

< form class="attributecolor" style="color:red"> method="attributevaluecolor" style="color:mediumblue">="post" action="attributevaluecolor" style="color:mediumblue">="="color:black">
="phptagcolor" style="color:red">< ?php class="phpkeywordcolor" style="color:mediumblue">echo class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" 
style="color:brown">'PHP_SELF'];class="phptagcolor" style="color:red">?>"="tagcolor" style="color:mediumblue">>
  Name: < input class="attributecolor" style="color:red"> type="attributevaluecolor" style="color:mediumblue">="text" name="attributevaluecolor" style="color:mediumblue">
  ="fname"="tagcolor" style="color:mediumblue">>
  < input class="attributecolor" style="color:red"> type="attributevaluecolor" style="color:mediumblue">="submit"="tagcolor" style="color:mediumblue">>
< /form class="tagcolor" style="color:mediumblue">>

< ?php
class="phpkeywordcolor" style="color:mediumblue">if (class="phpglobalcolor" style="color:goldenrod">$_SERVER[class="phpstringcolor" style="color:brown">"REQUEST_METHOD"] 
== class="phpstringcolor" style="color:brown">"POST") {
  class="commentcolor" style="color:green">// collect value of input field
class="phpnumbercolor" style="color:red">   $name = class="phpglobalcolor" style="color:goldenrod">$_POST[class="phpstringcolor" style="color:brown">'fname'];
  class="phpkeywordcolor" style="color:mediumblue">if (class="phpkeywordcolor" style="color:mediumblue">empty($name)) {
    class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"Name is empty";
  } class="phpkeywordcolor" style="color:mediumblue">else {
    class="phpkeywordcolor" style="color:mediumblue">echo $name;
class="phpnumbercolor" style="color:red">   }
}
class="phptagcolor" style="color:red">?>

< /body class="tagcolor" style="color:mediumblue">>
< /html class="tagcolor" style="color:mediumblue">> 

In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function!


PHP Superglobal - $_GET


Super global variables are built-in variables that are always available in all scopes.


PHP $_GET


PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:


< html>
< body>

<  a href="test_get.php?subject=PHP&web=mcatutorials.com">Test $GET< /a>

< /body>
< /html>

When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET.


The example below shows the code in "test_get.php":


Example
< html class="tagcolor" style="color:mediumblue">>
< body class="tagcolor" style="color:mediumblue">>

< ?php 
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"Study " . class="phpglobalcolor" style="color:goldenrod">$_GET[class="phpstringcolor" 
style="color:brown">'subject'] . class="phpstringcolor" style="color:brown">" at " . class="phpglobalcolor" style="color:goldenrod">$_GET[class="phpstringcolor" style="color:brown">
'web'];
class="phptagcolor" style="color:red">?>

< /body class="tagcolor" style="color:mediumblue">>
< /html class="tagcolor" style="color:mediumblue">>



Hi I am Pluto.