Super Global Variables In PHP

Here, we are going to examine PHP super global variables with examples and we will assess how they can be effectively used.

As a key feature of PHP, it can handle global variables, which can be used anywhere within a PHP script.

As part of this list of global variables, there is a set of special variables referred to as “superglobals”.

This list of variables is pre-defined by PHP and can be accessed from any scope at any time.

This feature makes it easy for you to easily access and edit important information such as user input, server information, and session data from the database.



PHP Super Global Variables

PHP super globals are a set of predefined variables that can be called in any scope as they are always available.

PHP automatically creates these variables so that they cannot be overwritten by user code and can only be modified by PHP.

Using super global, users can access information that has been entered by users, server details, and session information by using an interface.

PHP super globals has several types, each with a different purpose and functionality.

PHP super global variables consist of the following:

  1. $GLOBALS
  2. $_SERVER
  3. $_REQUEST
  4. $_POST
  5. $_GET
  6. $_FILES
  7. $_ENV
  8. $_COOKIE
  9. $_SESSION

PHP Super Global – GLOBALS

$GLOBALS is one of the superglobals provided by PHP. There are a number of variables that can be accessed through this superglobal.

These variables have been defined in the global scope of your PHP script, which you can then access.

In this section we will explore the various uses of the $GLOBALS super global in PHP programming by taking a closer look at it and exploring how to use it.


PHP $GLOBALS

There is a global variable in PHP that can be used in any part of the code. One of these global variables is $GLOBALS.

PHP Super Global allow you to access variables in the global scope of your PHP script. By doing this, it is very easy for users to use functions and methods within a module to access and modify global variables.

There is an array named $GLOBALS[index] in PHP that stores all the global variables that are affected by PHP.

There is an index in a variable that holds the name of the variable.

Using the example below, you will see how to use the super global variable $GLOBAL:

Example: 

<?php $a = 43; $b = 17; function sum() { $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b']; } sum(); echo $c; ?>

The variable c in the example above is also accessible outside the function, since it belongs to the $GLOBALS array!

In an example below, we are using $GLOBAL variable to edit array in editableArray() function:

Example: 

<?php $arr = array(1, 5, 4);function editableArray() { $GLOBALS['arr']['0'] = 6; } editableArray(); print_r($arr); ?>

Example Explanation

Above example defines an array $arr with three values (1, 5, 4). It then defines a function editableArray() which uses the $GLOBALS super global to modify the first value of the $arr array from 1 to 6.

When the editableArray() function is called, it uses the $GLOBALS super global to modify the first value of $arr.

The $GLOBALS super global is a special type of variable that is always available and allows you to access global variables from anywhere in your PHP code. In this case, we’re using $GLOBALS to access the $arr variable and modify its first element.

After modifying the first element of $arr, the function returns and the modified array is printed to the screen using the print_r() function. The output of print_r($arr) will be:

PHP Super Globals global

As you can see, the first element of the $arr array has been changed from 1 to 6, just as we modified it in the editableArray() function using the $GLOBALS super global.


PHP Super Globals – SERVER

There are a few super globals that are commonly used. One of the most popular is $_SERVER. This super global provides access to a variety of server and environment variables.

Basically, $_SERVER is an array that contains information about the current environment in which the script is being executed. This information includes the web server, the client’s IP address, the request method, and so on, in order to carry out that script.

From this section of article we are examining  PHP super global $_SERVER – we have also discussed some common use cases of it.

PHP Super Global $_SERVER

As a super global variable, $_SERVER is an array of information that is stored in PHP. It provides information about the server and the environment in which the script was run. There are a number of different types of information contained in it, such as headers, script paths, and port numbers.

Using it in PHP programs can give you a number of detailed information about the current environment on your system.

Example: 

<?php echo $_SERVER['PHP_SELF']; echo " "; echo $_SERVER['SERVER_NAME']; echo " "; echo $_SERVER['SCRIPT_NAME']; echo " "; echo $_SERVER['SERVER_ADDR']; echo " "; echo $_SERVER['SERVER_SOFTWARE']; echo " "; echo $_SERVER['SERVER_PROTOCOL']; echo " "; echo $_SERVER['HTTP_HOST']; echo " "; echo $_SERVER['HTTP_REFERER']; echo " "; echo $_SERVER['HTTP_USER_AGENT']; echo " "; ?>

Listed below is a table that illustrates some of the most important elements that can be placed inside $_SERVER with regards to PHP Super Globals Server:

ElementsOverview
$_SERVER[‘PHP_SELF’]This function returns the name of the script that is currently being
executed as a filename
$_SERVER[‘GATEWAY_INTERFACE’]This function returns the value that indicates what version of the
Common Gateway Interface (CGI) the server is using
$_SERVER[‘SERVER_ADDR’]This function returns the host server’s IP address as the value of
the argument
$_SERVER[‘SERVER_NAME’]This function returns the name of the server serving the request (for
example, the domain name mrexamples.com).
$_SERVER[‘SERVER_SOFTWARE’]This function returns the string that identifies the server (like
Apache/2.2.24)
$_SERVER[‘SERVER_PROTOCOL’]This function returns the information protocol’s name and revision
(for example, HTTP/1.1 is returned).
$_SERVER[‘REQUEST_METHOD’]In this method, the request method, such as POST, is returned, which
indicates how the page was accessed.
$_SERVER[‘REQUEST_TIME’]This function returns the timestamp at which the request was
initiated (for example, 1377687496).
$_SERVER[‘QUERY_STRING’]The query string is returned if a query string is used to access the
page
$_SERVER[‘HTTP_ACCEPT’]This function returns the Accept header of the current request.
$_SERVER[‘HTTP_ACCEPT_CHARSET’]There will be a return value for the Accept_Charset header from the
current request (such as UTF-8, ISO-8859-1).
$_SERVER[‘HTTP_HOST’]This function returns the host header from the current request that
was made.
$_SERVER[‘HTTP_REFERER’]This method gives you the complete URL of the current page (not
reliable due to the fact that not all user agents support this
method).
$_SERVER[‘HTTPS’]Would the script be accessed through a secure HTTP protocol if it
were accessed
$_SERVER[‘REMOTE_ADDR’]This function returns the IP address of the computer from which the
user is viewing the current page
$_SERVER[‘REMOTE_HOST’]This function returns the name of the host from which the current
page is being viewed by the user
$_SERVER[‘REMOTE_PORT’]You can use this method to find out what port the web server is using
to communicate with the user’s machine
$_SERVER[‘SCRIPT_FILENAME’]This function returns the absolute pathname of the script that is
currently being executed
$_SERVER[‘SERVER_ADMIN’]This function returns the value that has been set for the
SERVER_ADMIN directive in the web server configuration file (this
value will be the value that your script runs on a virtual host if
your script runs on one defined for that virtual host) (i.e.
[email protected] for instance)
$_SERVER[‘SERVER_PORT’]This method returns the port that is currently being used by the web
server for communication (such as 80) on the server machine
$_SERVER[‘SERVER_SIGNATURE’]The function returns the server version and virtual host name that
have been added to the server-generated pages.
$_SERVER[‘PATH_TRANSLATED’]This method returns the path to the current script based on the file
system
$_SERVER[‘SCRIPT_NAME’]This function returns the path to the script that is currently
running
$_SERVER[‘SCRIPT_NAME’]This function returns the URI of the current page on the server

PHP Super Global – REQUEST

The $_REQUEST variable contains the values of both $_GET and $_POST and also the values of any cookies that may be sent along with the request.

It can be very convenient to use $_REQUEST in situations where you want to use the same code for handling both GET and POST requests.

Please Note: Certain security risks associated with the use of $_REQUEST which can make your code vulnerable to attacks like cross-site scripting (XSS) and SQL injection if it isn’t configured properly, so it’s important to be aware of them as well.

PHP $_REQUEST

The $_REQUEST variable is a PHP super global variable that is responsible for collecting data on submission of an HTML form to the server.

In example below, you will see a form with an input field and a submit button at the bottom. A user clicking the submit button sends the form data to the file specified in the action attribute of the tag.

As an example, we will point the form data to this file in order to process the data in the form. You can replace this PHP file with the name of the PHP file you wish to use to process the form data.

To collect the value of the input field in response to the request, we can use PHP super global variable $_REQUEST as follows:

Example: 

<html> <body> <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") { // collecting input data $u_name= $_REQUEST["username"]; if (empty($u_name)) { echo "Name is empty"; } else { echo $u_name; } } ?> </body> </html>
Let’s take a look at another example here to give you a better understanding:

Example: 

<html> <body> <h3>User Details</h3> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form><?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_REQUEST['name']; $age = $_REQUEST['age']; if (empty($name) && empty($age)) { echo "Every field is required"; } else { echo "I am $name and my age is $age"; } } ?></body> </html>

Example Explanation

Above example includes a simple form for capturing user details such as name and age. The form uses the HTTP POST method to submit the data to the same page it is displayed on using the action attribute of the form tag. The action attribute is set to <?php echo $_SERVER[‘PHP_SELF’]; ?>, which is a special PHP constant that refers to the current page.

After the form, we use the $_REQUEST super global to retrieve the values of the name and age input fields. If the request method is POST, which is checked using the $_SERVER[‘REQUEST_METHOD’] superglobal, and both the name and age fields have been filled in, the script outputs a message that includes the submitted name and age.

If both of the fields is empty, the script outputs an error message asking the user to fill in all the required fields.

PHP Super Global – $_POST

The $_POST variable is one of the most commonly used PHP super global variables, which contains the data that is submitted via the HTTP POST method through the use of an HTML form.

In section we have explained the $_POST super global variable in PHP, including how it is used to process form data, retrieve values, and perform validation on the data.

It is essential for you, regardless of whether you are inexperienced with PHP or a seasoned developer, to be able to work with the $_POST super global variable.

PHP $_POST

The $_POST variable refers to a PHP super global variable which is used to collect form data once the HTML form has been submitted with the method equal to “post“.

It is also commonly used to pass variables to the server through $_POST.

Below you will find an example of a form in which we have an input field and a submit button.

If you click on the “Submit” button on the form, the form data is sent to the specified file in the action attribute of the form tag that gets attached to the form element.

The following example shows how to reference a file in order to process data from a form.

Using PHP super global variable _POST, we can use it to collect data from the input field in the following way:

Example: 

<html> <body> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Username: <input type="text" name="username" /> <input type="submit" /> </form> <?phpif (isset($_POST['username'])) { $u_name = $_POST['username']; if (empty($u_name)) { echo "Name is required!"; } else { echo $u_name; } } ?></body> </html>
Let’s take a look at another example here to give you a better understanding:

Example: 

<html> <body> <h3>User Details</h3> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form><?phpif (isset($_POST['name']) && isset($_POST['age'])) { $name = $_POST['name']; $age = $_POST['age']; if (empty($name) && empty($age)) { echo "Every field is required"; } else { echo "I am $name and my age is $age"; } } ?></body> </html>

Example Explanation

Above example is similar to the previous one. However, it uses the HTTP POST method to submit the form data to the server instead of the GET method. When the user submits the form, the form data is sent to the server via the POST method. The data is then processed by the PHP code that follows the form.Overall, this example demonstrates how to use the $_POST super global in PHP to retrieve data from an HTML form and process it. It also shows how to use the isset() and empty() functions to validate user input and handle errors.

The use of the POST method can be useful in cases where sensitive information is being transmitted, as it is not visible in the URL.

PHP Super Global – $_GET

In this section of article we will take a closer look at one of the most important PHP super global, $_GET. We will show you how to use $_GET at its highest level to maximize your results.

The $_GET is a method by which you can retrieve data from the web server through HTTP GET.

When you use $_GET, you can retrieve the values for the query parameters that were defined in the URL to access your PHP script. Parameters like this are usually used when passing data from one page to another, for example, user inputs or search queries between pages.


PHP $_GET

The PHP $_GET function is used to collect form data when an HTML form with method=”get” has been submitted.

Data sent in the URL can also be collected with $_GET.

Let’s say we have a HTML document that contains a hyperlink with a set of parameters inside:

 

<html>
<body>
<a href=”practice_get.php?title=PHP&link=mrexamples.com”>Practice $GET</a>

</body>
</html>

 

If the user clicks on the link “Practice $GET”, the parameters “title” and “link” will be sent to “practice_get.php“, and you will be able to get their values in practice_get.php by using the variable $_GET.

Here is an example of code that was used in the file called “practice_get.php“:

Example: 

<html> <body> <a href="practice_get.php?title=PHP&link=mrexamples.com">Practice $GET
Below is another example of $_GET to get a more clear understanding:

Example: 

<html> <body> <h3>User Details</h3> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form><?php if (isset($_GET['name']) && isset($_GET['age'])) { $name = $_GET['name']; $age = $_GET['age']; if (empty($name) & empty($age)) { echo "Every field is required"; } else { echo "I am $name and my age is $age"; } } ?> </body> </html>

Example Explanation

Above example is a basic HTML form that prompts the user to enter their name and age. The form uses PHP GET method to submit the user’s input to the server for processing.

When the user submits the form, the PHP code that follows the form is executed.

First it will check whether the ‘name’ and ‘age’ parameters are set in the $_GET super global array using the isset() function. If both parameters are set, the code assigns their values to two local variables $name and $age, respectively.

Then code checks whether both fields are empty using the empty() function.

If both fields are blank, the code outputs a message indicating that both fields are required. If both fields are not blank, the code outputs a message that includes the user’s name and age.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *