WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Global Variables - Superglobals


Superglobals were presented in PHP 4.1.0, and are inherent factors that are constantly accessible in all scopes.


The worldwide watchword is utilized to get to a worldwide variable from inside a function.

To do this, utilization the worldwide catchphrase inside the capacity, before the factors.

PHP Global Variables - Superglobals

Several predefined factors in PHP are "superglobals", which implies that they are constantly open, paying little mind to scope - and you can get to them from any capacity, class or record without doing anything special.

The PHP superglobal factors are:

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

This part will clarify a portion of the superglobals, and the rest will be clarified in later parts.


PHP $GLOBALS

$GLOBALS is a PHP too worldwide variable which is utilized to get to worldwide factors from anyplace in the PHP content (likewise from inside capacities or strategies).

PHP stores every single worldwide variable in a cluster called $GLOBALS[index]. The index holds the name of the variable.

The model beneath tells the best way to utilize the excessively worldwide variable $GLOBALS:

Example

<?php
$x = 50;
$y = 25;
 
function expansion() {
    $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
 
addition();
echo $z;
?>
Run model »

In the model above, since z is a variable present inside the $GLOBALS exhibit, it is additionally available from outside the capacity!


PHP $_SERVER

$_SERVER is a PHP too worldwide variable which holds data about headers, ways, and content areas.

The precedent beneath tells the best way to utilize a portion of the components in $_SERVER:

Example

<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
reverberation $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
reverberation "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
Run model »

The following table records the most imperative components that can go inside $_SERVER:

Element/Code Description
$_SERVER['PHP_SELF'] Returns the filename of the at present executing script
$_SERVER['GATEWAY_INTERFACE'] Returns the rendition of the Common Gateway Interface (CGI) the server is using
$_SERVER['SERVER_ADDR'] Returns the IP address of the host server
$_SERVER['SERVER_NAME'] Returns the name of the host server, (for example, www.welookups.com)
$_SERVER['SERVER_SOFTWARE'] Returns the server recognizable proof string, (for example, Apache/2.2.24)
$_SERVER['SERVER_PROTOCOL'] Returns the name and amendment of the data convention, (for example, HTTP/1.1)
$_SERVER['REQUEST_METHOD'] Returns the demand technique used to get to the page, (for example, POST)
$_SERVER['REQUEST_TIME'] Returns the timestamp of the beginning of the demand, (for example, 1377687496)
$_SERVER['QUERY_STRING'] Returns the question string if the page is gotten to by means of an inquiry string
$_SERVER['HTTP_ACCEPT'] Returns the Accept header from the present demand
$_SERVER['HTTP_ACCEPT_CHARSET'] Returns the Accept_Charset header from the present demand, (for example, utf-8,ISO-8859-1)
$_SERVER['HTTP_HOST'] Returns the Host header from the present demand
$_SERVER['HTTP_REFERER'] Returns the total URL of the present page (not dependable in light of the fact that not all client operators support it)
$_SERVER['HTTPS'] Is the content questioned through a safe HTTP protocol
$_SERVER['REMOTE_ADDR'] Returns the IP address from where the client is seeing the current page
$_SERVER['REMOTE_HOST'] Returns the Host name from where the client is seeing the current page
$_SERVER['REMOTE_PORT'] Returns the port being utilized on the client's machine to speak with the web server
$_SERVER['SCRIPT_FILENAME'] Returns the supreme pathname of the as of now executing script
$_SERVER['SERVER_ADMIN'] Returns the esteem given to the SERVER_ADMIN mandate in the web server arrangement record (if your content keeps running on a virtual host, it will be the esteem characterized for that virtual host, (for example, someone@welookups.com)
$_SERVER['SERVER_PORT'] Returns the port on the server machine being utilized by the web server for correspondence, (for example, 80)
$_SERVER['SERVER_SIGNATURE'] Returns the server rendition and virtual host name which are added to server-created pages
$_SERVER['PATH_TRANSLATED'] Returns the record framework based way to the current script
$_SERVER['SCRIPT_NAME'] Returns the way of the current script
$_SERVER['SCRIPT_URI'] Returns the URI of the current page

PHP $_REQUEST

PHP $_REQUEST is utilized to gather information in the wake of presenting a HTML form.

The precedent underneath demonstrates a structure with an info field and a submit catch. At the point when a client presents the information by tapping on "Submit", the structure information is sent to the record indicated in the activity property of the <form> tag. In this model, we point to this record itself for preparing structure information. On the off chance that you wish to utilize another PHP record to process structure information, supplant that with the filename of your decision. At that point, we can utilize the too worldwide variable $_REQUEST to gather the estimation of the info field:

Example

<html>
<body>

<form method="post" action="<?php reverberation $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    /gather estimation of information field
    $name = $_REQUEST['fname'];
    on the off chance that (empty($name)) {
        reverberation "Name is empty";
    } else {
        reverberation $name;
    }
}
?>

</body>
</html>
Run model »

PHP $_POST

PHP $_POST is generally used to gather structure information subsequent to presenting a HTML structure with method="post". $_POST is likewise generally used to pass variables.

The model beneath demonstrates a structure with an info field and a submit catch. At the point when a client presents the information by tapping on "Submit", the structure information is sent to the document indicated in the activity trait of the <form> tag. In this precedent, we point to the document itself for preparing structure information. In the event that you wish to utilize another PHP document to process structure information, supplant that with the filename of your decision. At that point, we can utilize the excessively worldwide variable $_POST to gather the estimation of the info field:

Example

<html>
<body>

<form method="post" action="<?php reverberation $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    /gather estimation of information field
    $name = $_POST['fname'];
    in the event that (empty($name)) {
        reverberation "Name is empty";
    } else {
        reverberation $name;
    }
}
?>

</body>
</html>
Run precedent »

PHP $_GET

PHP $_GET can likewise be utilized to gather structure information in the wake of presenting a HTML structure with method="get".

$_GET can likewise gather information sent in the URL.

Assume we have a HTML page that contains a hyperlink with parameters:

<html>
<body>

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

</body>
</html>

When a client taps on the connection "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you would then be able to get to their qualities in "test_get.php" with $_GET.

The model beneath demonstrates the code in "test_get.php":

Example

<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>
Run model »