Site Admin
Site Admin Founder of MeaningArticles
27 Views

PHP Global Variables - Superglobals

Hello Dev.

Nowadays i'm able to describe a way to use php global variables and superglobale. this article is learn you implementation of global variables and superglobals. Superglobals is introduce in PHP 4.1.0, variables which are usually to be had built-in all scopes.

So let's start the lesson...


What is superglobals in PHP?

Superglobal Variables in PHP are predefined global variables. Global variables are variables with global scope, which means that they can be used wherever needed – they do not need to be declared, nor do they need to be marked with global in functions. All superglobal variables are written in all-caps E.g. $_GLOBALS, $_SERVER, $_REQUEST.

The PHP Superglobal Variables
All of the superglobal variables act as associative arrays that use a string value as a key to access values. The following is a list of superglobal variables in PHP:

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

$GLOBALS
$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script.

<!DOCTYPE html>
<html>
    <body>
        <?php 
            $x = 75;
            $y = 25; 
            function addition() {
            $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
            }
            addition();
            echo $z;
        ?>
    </body>
</html>

Output:

100

 

$_SERVER
$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

<!DOCTYPE html>
<html>
    <body>
        <?php 
            echo $_SERVER['PHP_SELF'];
            echo "<br>";
            echo $_SERVER['SERVER_NAME'];
            echo "<br>";
            echo $_SERVER['HTTP_HOST'];
            echo "<br>";
            echo $_SERVER['HTTP_REFERER'];
            echo "<br>";
            echo $_SERVER['HTTP_USER_AGENT'];
            echo "<br>";
            echo $_SERVER['SCRIPT_NAME'];
        ?>
    </body>
</html>

Output:

/demo/demo_global_server.php
35.194.26.41
35.194.26.41
https://tryphp.meaningartilces.com/showphp.php?filename=demo_global_server
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0
/demo/demo_global_server.php

 

$_REQUEST
PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form.

<!DOCTYPE html>
<html>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            Name: <input type="text" name="fname">
            <input type="submit">
        </form>
        <?php 
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                // collect value of input field
                $name = $_REQUEST['fname'];
                if (empty($name)) {
                echo "Name is empty";
                } else {
                echo $name;
                }
            }
        ?>
    </body>
</html>

Output:

abc

 

$_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.

<!DOCTYPE html>
<html>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            Name: <input type="text" name="fname">
            <input type="submit">
        </form>
        <?php 
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                // collect value of input field
                $name = $_REQUEST['fname'];
                if (empty($name)) {
                echo "Name is empty";
                } else {
                echo $name;
                }
            }
        ?>
    </body>
</html>

Output:

abc

 

$_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.

<!DOCTYPE html>
<html>
    <body>
        <a href="test_get.php?subject=PHP&web=meaningarticles.com">Click $GET</a>
    </body>
</html>

Output:

Click $GET

 

$_FILES
PHP $_FILES is a two-dimensional associative array that contains a list of files that were uploaded to the script using the POST method. The keys to this array are the names of the fields uploading the files and the data being accessed. For example, $_FILES[fileUploaded][name] accesses the name of the file being uploaded from the fileUploaded field.

Following test.html contains a HTML form whose enctype is set to multiform/form-data. It also has an input file element which presents a button on the form for the user to select file to be uploaded.
test.html

<form action="testscript.php" method="POST" enctype="multipart/form-data">
   <input type="file" name="file">
   <input type ="submit" value="submit">
</form>

The PHP script is as follows:
testscript.php

<?php
echo "Filename: " . $_FILES['file']['name']."<br>";
echo "Type : " . $_FILES['file']['type'] ."<br>";
echo "Size : " . $_FILES['file']['size'] ."<br>";
echo "Temp name: " . $_FILES['file']['tmp_name'] ."<br>";
echo "Error : " . $_FILES['file']['error'] . "<br>";
?>

Output:

Filename: hello.html
Type : text/html
Size : 56
Temp name: C:\xampp\tmp\php32CE.tmp
Error : 0

 

$_COOKIES
PHP $_COOKIES keeps data input via HTTP Cookies. The keys to this array are defined when the cookies are set.

<?php
$cookie_value = "meaningarticles tutorials";
setcookie("meaningarticles", $cookie_value, time()+3600, "/home/your_usename/", "example.com", 1, 1);
if (isset($_COOKIE['cookie']))
echo $_COOKIE["meaningarticles"];
?>

Output:

meaningarticles tutorials

 

$_SESSION
PHP $_SESSION holds session variables. Session variables can be accessed on multiple pages. This array’s keys are defined by the users when they define session variables.

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

Output:

Session variables are set.

 

$_ENV
$_ENV contains information about the environment that PHP is running in. The keys to the values in this array are predefined.

getenv
Following script displays values of all available environment variables.

<?php
$arr=getenv();
foreach ($arr as $key=>$val)
echo "$key=>$val
";
?>

To obtain value of specific variable use its name as argument for getenv() function
Example:

<?php
echo "Path: " . getenv("PATH");
?>

Output:

Path: https://meaningarticles.com/

 

putenv
PHP putenv() Sets the value of an environment variable. 

<?php
putenv("PHP_TEMPUSER=TEST");
echo "Temp user: " . getenv("PHP_TEMPUSER");
?>

Output:

Temp user: TEST

 

I hope it's assist you, thanks for visit my article if you like my article then share with your friends on social media platform.
Happy Coding.....