EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Superglobal Variables

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Superglobal Variables

PHP Superglobal Variables

Introduction to PHP Superglobal Variables

In PHP, some of the variables are predefined in the package itself for them to be used in inappropriate places. These predefined variables play a major role in webpage development in PHP. These variables are called “superglobal” variables. The term globalization refers to the context that it can be accessed globally irrespective of the scope you can access it anywhere in the program. These variables are used in web page development in PHP and these variables have a predefined functionality and can be used whenever required.

List of PHP Superglobal Variables

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

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Some of the superglobal variables in PHP are as follows:

1. $GLOBALS

PHP $GLOBAL variable is used to use the value of the variable globally and can be used anywhere in the program. PHP stores the global variables in an array called $GLOBALS [index]. The index here plays an important role as it stores the name of the variables. So whenever we use the variable with the global keyword automatically the value will be set and operation will be performed.

Code:

<?php
$x = 75;
$y = 25;
$z = 100;
function addup()
{
$GLOBALS['a'] = $GLOBALS['x'] + $GLOBALS['y'] + $GLOBALS['z'];
}
addup();
echo $a;
?>

Output:

PHP Superglobal

2. $_SERVER

This superglobal variable is used to capture the information about headers, path locations, and scripts location. This variable is used in most of the places where the address and path locations have to be printed.

Some of them are as follows:

  • $_SERVER[‘PHP_SELF’]: This variable is used to print or return the current filename that is getting executed.
  • $_SERVER[‘SERVER_ADDR’]: This variable is used to return the Internet Protocol(IP) address of the host server.
  • $_SERVER[‘REQUEST_METHOD’]: This variable is used to return the method that has been used in the program for submission i.e. either GET or POST.
  • $_SERVER[‘SCRIPT_NAME’]: This variable returns the path of the current script that the user is running for execution.

Code:

<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_ADDR'];
echo "<br>";
?>

Output:

PHP Superglobal

3. $_REQUEST

This super global variable is used in the code to return the data i.e. collection of data when a form is submitted. This variable is used when forms are used in the program. When the user fills the data in the form it is then submitted using any one method i.e. GET or POST. When the user submits the form using the GET method the content is visible and the data can be seen by the user. When the POST method is used the data is hidden and invisible to the user. The data can be collected with this super global variable $_REQUEST.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

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

Output:

PHP Superglobal

4. $_POST

This super global variable is used when the user has to submit the form and in the form action, we need to specify which type the form has to be submitted. This method collects the data after the user submits the form and the method specified in the form action will be $_POST. This method hides the data that has been entered by the user. This method is mostly used for login purposes, encryption, etc.

Code:

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

Output:

$ POST

5. $_GET

This super global variable is generally used to display the content or data entered by the user. This variable helps the data to be collected and makes it visible to the user. The user can see 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 specified in the form action method where the action has to be performed when the user submits the form.

Code:

<!DOCTYPE html>
<html>
<body>
<a href="demo_get.php?subject=PHP&web=google.com">Example for $GET</a>
</body>
</html>

Output:

$GET

Importance of Super Global Variable in PHP

The main objective of using super global variables in PHP is that the developer can easily use the built-in functions of PHP that comes with the package in the program. These variables are built with a meaning that has to be used properly in the program. These variables can be accessed anywhere whenever required. These variables collect the data or content entered by the user and display it wherever required. So, if a user wishes to see the information that he has entered in the form, the developer can use this variable in the program to collect the information that has been entered by the user and display it on the screen.

There are few more super global variables in PHP like $_COOKIE, $_SESSION, $_ ENV, etc. This variable also has its scope to be used in the program. Cookies are used by the developer to save the data in that session so that when the user logs in next time, the data is provided to him. Sessions are used in PHP to create a timeframe for a particular page to get timed out once the session gets expired. This is generally used for security reasons and is mostly used in banking, social media, etc.

Conclusion

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

Recommended Articles

This is a guide to PHP Superglobal Variables. Here we discuss the basic concept, list of PHP Superglobal Variables and the importance along with the examples. You can also go through our other related articles to learn more –

  1. PHP Login Template
  2. Login Page in PHP
  3. Protected in PHP
  4. PHP Array Search

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More