PHP Superglobal Variables - Informatic Point

PHP Superglobal Variables

PHP Superglobal Variables. In PHP, some of the variables are predefined in the package itself for them to be used in unsuitable positions. These predefined variables perform an important role in webpage development in PHP. These variables are called “superglobal variables“. The word globalization refers to the context that it can be obtained globally irrespective of the range you can reach anywhere in the program. PHP variables are utilized in web page development in PHP and these variables have predefined functionality and features that can be used whenever required.

List of PHP Superglobal Variables

  1. $GLOBALS
  2. $_REQUEST
  3. $_SERVER
  4. $_GET
  5. $_POST

The superglobal variables in PHP can be located anywhere and anytime whenever required for its functionality.

Some of the superglobal variables in PHP are as follows:

$GLOBALS

PHP $GLOBAL variable is used to use the purpose of the variable globally and can be used anyplace in the program. PHP saves the global variables in an array called $GLOBALS [index]. The index here performs a significant role as it saves the name of the variables. So whenever we do the variable with the global keyword automatically the value will be placed and operation will be performed.

<?php
$x = 85;
$y = 95;
$z = 100;
function softup()
{
$GLOBALS['a'] = $GLOBALS['x'] + $GLOBALS['y'] + $GLOBALS['z'];
}
softup();
echo $a;
?>
Output
280

$_REQUEST

This super global variable $_REQUEST is used in the code to return the data i.e. set of data when a report is submitted. This variable is utilized when reports/forms are used in the program. When the user fills the data in the form it is then presented using any one method i.e. GET or POST. When the user submits the form using the GET method the content is apparent and the data can be viewed by the user. When the POST process is used the data is hidden and invisible to the user. The data can be obtained with this variable.

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
username: <input type="text" name="username">
userid: <input type="text" name="euserid">
<input type="submit">
</form> <?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = htmlspecialchars($_REQUEST['username']);
$userid= htmlspecialchars($_REQUEST['euserid']);
if (empty($name)) {
echo "Employee Name is empty";}
else{
echo $name;}
if (empty($userid)) {
echo "Employee userid is empty";
} else {
echo $userid;
}}?>
Output
userid:

$_SERVER

This superglobal variable $_SERVER is used to catch the data about headers, path locations, and scripts location. This variable is used in most of the places where the location and path have to be printed.

Some of them are as follows:

  • $_SERVER[‘REQUEST_METHOD’]: This variable is used to render the method that has been used in the program for the submission form.
  • $_SERVER[‘SCRIPT_NAME’]: This variable renders the path of the current script that the user is working for execution.
  • $_SERVER[‘PHP_SELF’]: This variable is used to return the current filename that is getting runs.
  • $_SERVER[‘SERVER_ADDR’]: This variable is used to return the Internet Protocol(IP) address of the host server.
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_ADDR'];
echo "<br>";
?>
Output
/dev/index.php

$_GET

This super global variable $_GET is usually used to perform the content or data accessed by the user. This variable serves the data to be collected and makes it visible to the user. The user can view the data in the URL of the page. This variable is not secure and should not be used for security reasons. This variable is generally defined in the form action method where the action has to be executed when the user submits the form.

<a href="php_get.php?subject=PHP&web=google.com">E.g for $GET</a>
Output

$_POST

This super global variable $_POST is used when the user has to submit the form and in the form action, we require to define which type the form has to be presented. This method gets the data after the user submits the form and the method defined in the form action will be $_POST. This method hides the data that has been inserted by the user.

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
username: <input type="text" name="username">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_REQUEST['username']);
if (empty($name)) {
echo "Employee Name is empty";}
else{
echo $name;
}}?>
Output
username:

Conclusion

In this article, we explained what is a superglobal variable and its scope in the script. Also, we presented various types of superglobal variables and their functionality. The main intention of using these variables is that it can be globally used by the developer.