Hello Coders, today we learn about or gain some knowledge about PHP Superglobal Variables or superglobal variables in PHP. In the start of PHP programming we all use these predefined variables but always confused why these we use, some of the explanation with an example, I will show you today for better understanding. So let’s start these,
Complete Reference: PHP Superglobal Variables
$GLOBALS
This variable holds data with the array of a variable like $GLOBALS[]. It calls hold value using their element indexes. I elaborate this variable on the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $a = 50; $b = 10; function myFunction() { $GLOBALS['c'] = $GLOBALS['a'] - $GLOBALS['b']; } myFunction(); echo $c; ?> <strong>Ouptput:40</strong> |
On the above example, we define the $a and $b both are global variables which are stored in the variable $c which hold by Global array and call outside the function and also give o/p.
$_SERVER
$_SERVER is a global variable which stores the information about headers, paths and script location and also sometimes we use for getting data from the server. Some of the use of the $_SERVER is in below example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php echo $_SERVER['HTTP_USER_AGENT']; <strong>//output</strong>:<strong><span id=":3l.co" class="tL8wMe EMoHub" dir="ltr" style="color: #008000;">Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36</span></strong> echo $_SERVER['SCRIPT_NAME']; <strong>//output</strong>:<span style="color: #008000;"><strong>/admin/post-new.php</strong></span> echo $_SERVER['PHP_SELF']; <strong>//output</strong>:<span style="color: #008000;"><strong>/admin/post-new.php</strong></span> echo $_SERVER['HTTP_REFERER']; <strong>//output</strong>:<strong><span style="color: #008000;">www.codingtasks.net/admin/post-new.php</span></strong> echo $_SERVER['SERVER_NAME']; <strong>//output</strong>:<span style="color: #008000;"><strong>www.codingtasks.net</strong></span> echo $_SERVER['HTTP_HOST']; <strong>//output</strong>:<span style="color: #008000;"><strong>www.codingtasks.net</strong></span> ?> |
$_REQUEST
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> NAME: <input type="text" name="firstname"> <button type="submit">SUBMIT</button> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { <strong>//Getting value from a form when submit the form with POST method</strong> $firstname = $_REQUEST['firstname']; if(empty($name)){ echo "Nothing here"; } else { echo $firstname; } } ?> </body> </html> |
$_SESSION
This is very important and used very widely in PHP web programming for transferring data from one page to another page, $_SESSION holds the variable as an array, working with session_start() function. You can check below with an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php <strong><span style="color: #008000;">// Start the session</span></strong> session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["animal1"] = "dog"; $_SESSION["animal2"] = "cat"; echo "Session variables are set."; ?> </body> </html> |
$_ENV
This variable as a name suggests Environment variable and hold the environment variable also like if in you PHP script env variable is user name you can set it like $_ENV[‘username’] of the PHP script.
On such conditions, this not environment set variable because for setting env variable we use putenv() function.
1 2 3 4 5 6 |
<?php echo 'My username is ' .$_ENV["USER"] . '!'; ?> |
Others Superglobals are you already know very well like $_POST, $_GET.
If you have any improvement on this artical please comment below.
Happy Coding..!
[…] PHP Superglobal Variables […]
[…] To Check Page is Refreshed in PHP, we use PHP superglobal variable. […]
[…] the above code, we use PHP inbuilt superglobal variable $_SERVER which takes the user’s IP […]