• Home
  • HTML
    • HTML Introduction
    • HTML Basic
    • HTML Elements
    • HTML Attributes
    • HTML Headings
    • HTML Paragraphs
    • HTML Text Formatting
    • HTML Styles
    • HTML Comments
    • HTML Styles - CSS
    • HTML Links
    • HTML Images
    • HTML Tables
    • HTML Lists
    • HTML Block and Inline Elements
    • HTML class Attribute
    • HTML Forms
    • HTML Media
  • CSS
    • Introduction of CSS
    • CSS Syntax
    • CSS Selectors
    • How To Add CSS
    • CSS Comments
    • CSS Colors
    • CSS Backgrounds
    • CSS Borders
    • CSS Margins
    • CSS Text
    • CSS Lists
    • CSS Tables
    • CSS Box Model
    • CSS Dimension
    • CSS Padding
    • CSS Border
    • CSS Margin
    • CSS Outline
    • CSS Cursors
    • CSS Overflow
    • CSS Units
    • CSS Visual Formatting
    • CSS Display
    • CSS Visibility
    • CSS Position
    • CSS Layers
    • CSS Float
    • CSS Alignment
    • CSS Pseudo-classes
    • CSS Pseudo-elements
    • CSS Media Types
    • CSS Sprites
    • CSS Opacity
    • CSS Attribute Selectors
    • CSS Validation
    • CSS3 Border
    • CSS3 Gradients
    • CSS3 Text Overflow
  • JavaScript
    • JS Introduction
    • JS Getting Started
    • JS Syntax
    • JS Variables
    • JS Generating Output
    • JS Data Types
    • JS Operators
    • JS Events
    • JS Strings
    • JS Numbers
    • JS If Else
    • JS Switch Case
    • JS Arrays
    • JS Sorting Arrays
    • JS Loops
    • JS Functions
    • JS Objects
    • JS DOM Nodes
    • JS DOM Selectors
    • JS DOM Styling
    • JS DOM Get Set Attributes
    • JS DOM Manipulation
    • JS DOM Navigation
    • JS Window
    • JS Screen
    • JS Location
    • JS History
    • JS Navigator
    • JS Dialog Boxes
    • JS Timers
    • JS Date and Time
    • JS Math Operations
    • JS Type Conversions
    • JS Event Listeners
    • JS Event Propagation
    • JS Borrowing Methods
    • JS Hoisting Behavior
    • JS Closures
    • JS Strict Mode
    • JS JSON Parsing
    • JS Error Handling
    • JS Regular Expressions
    • JS Form Validation
    • JS Cookies
    • JS AJAX Requests
    • JS ES6 Features
  • jQuery
    • jQuery Introduction
    • jQuery Syntax
    • jQuery Selectors
    • jQuery Events
    • jQuery Show/Hide
    • jQuery Fade
    • jQuery Slide
    • jQuery Animation
    • jQuery Stop
    • jQuery Chaining
    • jQuery Callback
    • jQuery Get/Set
    • jQuery Insert
    • jQuery Remove
    • jQuery CSS Classes
    • jQuery Style Properties
    • jQuery Dimensions
    • jQuery Traversing
    • jQuery Ancestors
    • jQuery Descendants
    • jQuery Siblings
    • jQuery Filtering
    • jQuery Ajax
    • jQuery Load
    • jQuery Get/Post
    • jQuery No-Conflict
  • PHP
    • PHP Introduction
    • PHP Install
    • PHP Syntax
    • PHP Comments
    • PHP Variables
    • PHP Echo / Print
    • PHP Data Types
    • PHP Strings
    • PHP Constants
    • PHP Operators
    • PHP If...Else...Elseif
    • PHP Switch
    • PHP Loops
    • PHP Functions
    • PHP Arrays
    • PHP Superglobals
    • PHP Date and Time
    • PHP Include
    • PHP File Handling
    • PHP File Upload
    • PHP Cookies
    • PHP Sessions
    • PHP Filters
    • PHP Callback Functions
    • PHP JSON
    • PHP Exceptions
    • PHP What is OOP
    • PHP Classes/Objects
    • PHP Constructor
    • PHP Destructor
    • PHP Access Modifiers
    • PHP Inheritance
    • PHP Abstract Classes
    • PHP Interfaces
    • PHP Traits
    • PHP Static Methods
    • PHP Namespaces
  • SQL
    • Introduction to SQL
    • SQL Create Command
    • SQL ALTER Command
    • SQL Truncate Drop Rename
    • INSERT SQL command
    • UPDATE SQL command
    • DELETE SQL command
    • SQL COMMIT command
    • SQL ROLLBACK command
    • SQL GRANT and REVOKE Command
    • SQL WHERE clause
    • SQL LIKE clause
    • SQL ORDER BY Clause
    • SQL Group By Clause
    • SQL HAVING Clause
    • SQL DISTINCT keyword
    • SQL AND OR operator
    • SQL Constraints
    • SQL Functions
    • SQL JOIN
  • Python
    • Getting started with Python
    • Introduction to IDLE
    • Python 2.x vs. Python 3.x
    • Syntax Rules and First Program
    • Numbers and Math Functions
    • Python Operators
    • Python Variables
    • Python Modules and Functions
    • Python Input and Output
    • Data Types in Python
    • String in Python
    • String Functions in python
    • Lists in Python
    • Utilizing List Elements by Iterating
    • Deleting List Elements & other Functions
    • Dictionaries in Python
    • Functions for Dictionary
    • Tuples in Python
    • Relational and Logical Operators
    • Conditional Statements in Python
    • Looping in Python
    • Define Functions in Python
    • Python-Introduction to OOP
    • Object Oriented Programming in Python
    • Classes in Python
    • The concept of Constructor
    • Destructors - Destroying the Object in Python
    • Inheritance in Python
    • Access Modifers in Python
    • Types of Inheritance
    • Method Overriding in Python
    • Polymorphism
    • static Keyword
    • Operator Overloading Python
    • Introduction to Error Handling
    • Exception Handling: try and except
    • Exeption Handling: finally
    • Exception Handling: raise
    • File Handling
    • Reading and Writing File
    • Introduction to Multithreading
    • Threading Module in Python
    • Thread Object
    • Lock Object
    • RLock Object
    • Event Object
    • Timer Object
    • Condition Object
    • Barrier Object
    • __name__ Variable in Python
    • Iterable and Iterator
    • yield Keyword
    • Python Generators
    • Python Closures
    • Python Decorators
    • @property Decorator in Python
    • Assert Statement
    • Garbage Collection
    • Shallow and Deep Copy
    • Introduction to Logging
    • Configure Log LEVEL, Format etc
    • Python Logging in a file
    • Python Logging Variable Data
    • Python Logging Classes and Functions
    • Python MySQL Introduction
    • Create Database - Python MySQL
    • Create Table - Python MySQL
    • Insert Data in Table
    • Select Data from Table
    • Update data in Table
    • Delete data from Table
    • Drop Table from Database
    • WHERE clause - Python MySQL
    • Order By clause - Python MySQL
    • Limit clause - Python MySQL
    • Table Joins - Python MySQL
  • MongoDB
    • MongoDB Introduction
    • Overview of MongoDB
    • MongoDB vs SQL Databases
    • Advantages of MongoDB
    • When to go for MongoDB
    • Data Modelling in MongoDB
    • Is MongoDB really Schemaless?
    • Installing MongoDB on Windows and Linux
    • Datatypes in MongoDB
    • Create and Drop Database in MongoDB
    • MongoDB: Creating a Collection
    • CRUD Operations in MongoDB
    • Data Relationships in MongoDB
    • Indexing in MongoDB
    • Sorting in MongoDB
    • Aggregation in MongoDB
    • Data Backup and Restoration in MongoDB
    • Sharding in MongoDB
    • Java Integration with MongoDB
  • Elixir
    • Elixir Overview
    • Elixir Environment
    • Elixir Basic Syntax
    • Elixir Data Types
    • Elixir Variables
    • Elixir Operators
    • Elixir Pattern Matching
    • Elixir Decision Making
    • Elixir Strings
    • Elixir Char Lists
    • Elixir Lists and Tuples
    • Elixir Keyword Lists
    • Elixir Maps
    • Elixir Modules
    • Elixir Aliases
    • Elixir Functions
    • Elixir Recursion
    • Elixir Loops
    • Elixir Enumerables
    • Elixir Streams
    • Elixir Structs
    • Elixir Protocols
    • Elixir File I/O
    • Elixir Processes
    • Elixir Sigils
    • Elixir Comprehensions
    • Elixir Typespecs
    • Elixir Behaviours
    • Elixir Errors Handling
    • Elixir Macros
    • Elixir Libraries
  • TypeScript
    • TypeScript Overview
    • Install TypeScript
    • First TypeScript Program
    • Type Annotation
    • TypeScript Variable
    • TypeScript Data Type Number
    • TypeScript Data Type String
    • TypeScript Data Type Boolean
    • TypeScript Arrays
    • TypeScript Tuples
    • TypeScript Enum
    • TypeScript Union
    • TypeScript Any Data Type
    • TypeScript Void Data Type
    • TypeScript Never Data Type
  • Home
  • PHP Introduction
  • PHP Install
  • PHP Syntax
  • PHP Comments
  • PHP Variables
  • PHP Echo / Print
  • PHP Data Types
  • PHP Strings
  • PHP Constants
  • PHP Operators
  • PHP If...Else...Elseif
  • PHP Switch
  • PHP Loops
  • PHP Functions
  • PHP Arrays
  • PHP Superglobals
  • PHP Date and Time
  • PHP Include
  • PHP File Handling
  • PHP File Upload
  • PHP Cookies
  • PHP Sessions
  • PHP Filters
  • PHP Callback Functions
  • PHP JSON
  • PHP Exceptions
  • PHP What is OOP
  • PHP Classes/Objects
  • PHP Constructor
  • PHP Destructor
  • PHP Access Modifiers
  • PHP Inheritance
  • PHP Abstract Classes
  • PHP Interfaces
  • PHP Traits
  • PHP Static Methods
  • PHP Namespaces
Home >> php >> PHP Superglobals

PHP Superglobals

This PHP Superglobals tutorial helps you learn the concept of superglobal variables.

 

What is PHP Superglobals?

Superglobals are some predefined variables in PHP. They are always accessible, irrespective of scope. These can be accessed by you from any function, class, or file without doing anything special.

 

Following are the PHP superglobal variables:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

 

PHP $GLOBALS

This PHP superglobal variable is used for accessing global variables from anywhere in the PHP script (even from within functions or methods).

All global variables are stored by PHP in an array called $GLOBALS[index]. The name of the variable is held by the index.

Example:

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

Output:

100

Note: In the above-mentioned example, since z is a variable present within the $GLOBALS array, you can access it from outside the function!

 

PHP $_SERVER

This PHP superglobal variable holds information about headers, paths, and script locations.


Example:

<?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'];
?>

Output:

This will depends on your server

 

The table given below lists the most important elements/code that can go in $_SERVER:

Element/CodeDescription
$_SERVER['PHP_SELF']It will return the filename of the currently executing script
$_SERVER['GATEWAY_INTERFACE']

It will return the version of the Common Gateway Interface (CGI) being used by 

the server.

$_SERVER['SERVER_ADDR']It will return the host server's IP address 
$_SERVER['SERVER_NAME']It returns the host server's name
$_SERVER['SERVER_SOFTWARE']The server identification string (such as Apache/2.2.24) is returned by it
$_SERVER['SERVER_PROTOCOL']It will return the information protocol's name and revision (such as HTTP/1.1)
$_SERVER['REQUEST_METHOD']It will return the request method used for accessing the page (such as POST)
$_SERVER['REQUEST_TIME']It will return the timestamp of the start of the request (such as 1377687496)
$_SERVER['QUERY_STRING']It will return the query string if the page is accessed via a query string
$_SERVER['HTTP_ACCEPT']It will return the Accept header from the current request
$_SERVER['HTTP_ACCEPT_CHARSET']

It will return the Accept_Charset header from the current request 

(such as utf-8,ISO-8859-1)

$_SERVER['HTTP_HOST']It will return the Host header from the current request
$_SERVER['HTTP_REFERER']

It returns the current page's complete URL

 (unreliable because not all user-agents support it)

$_SERVER['HTTPS']It is the script queried via a secure HTTP protocol
$_SERVER['REMOTE_ADDR']It will return the IP address from where the user is viewing the current page
$_SERVER['REMOTE_HOST']It will return the Hostname from where the user is viewing the current page
$_SERVER['REMOTE_PORT']It will return the port that the user's machine is using to
$_SERVER['SCRIPT_FILENAME']It returns the currently executing script's absolute pathname
$_SERVER['SERVER_ADMIN']

It returns the value given to the SERVER_ADMIN directive in the 

web server configuration file (in case your script runs on a virtual host, then it 

will be the value set for that virtual host) (like [email protected])

$_SERVER['SERVER_PORT']

It will return the port on the server machine the web server is using for

 communication (such as 80)

$_SERVER['SERVER_SIGNATURE']

It will return the server version and virtual host name, which are 

added to server-generated pages

$_SERVER['PATH_TRANSLATED']It will return the file system based path to the current script
$_SERVER['SCRIPT_NAME']It returns the current script's path
$_SERVER['SCRIPT_URI']It will return the URI of the current page

 

PHP $_REQUEST

This PHP superglobal variable is used for collecting data after submitting an HTML form.

The below-mentioned example depicts a form with an input field and a submit button. When the data is submitted by a user by clicking on "Submit", it will send the form data to the file specified in the <form> tag's action attribute. We point to this file itself to process form data in the example here. In case you want to use a different PHP file for processing form data, you can replace it with your choice of filename. Then, the superglobal variable $_REQUEST can be used to collect the value of the input field.

Example:

<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>

 

PHP $_POST

This PHP superglobal variable collects form data after submitting an HTML form with method="post". It is also used to pass variables.

 

The below-mentioned example depicts a form with an input field and a submit button. When the data is submitted by a user by clicking on "Submit", it sends the form data to the file specified in the <form> tag's action attribute. We point to this file itself to process form data in the example here. In case you want to use a different PHP file for processing form data, you can replace it with your choice of filename. Then, the superglobal variable $_POST can be used to collect the value of the input field.

<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 = $_POST['fname'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>

</body>
</html>

 

PHP $_GET

This PHP superglobal variable collects form data after submitting an HTML form with the method="get".

It also collects data sent in the URL.

Now, assume you have an HTML page containing a hyperlink with parameters:

<html>
<body>

<a href="test_get.php?subject=PHP&web=developerstutorial.com">Test $GET</a>

</body>
</html>

When the link "Test $GET" is clicked by a user, it will send the parameters "subject" and "web" to "test_get.php", and their values in "test_get.php" can be accessed by you with $_GET.

The below-mentioned example shows the code in "test_get.php":

<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>

 

 

  • Prev
  • Next


-Advertisement-


DeveloperTutorial
[email protected] © 2022-2023 Developers Tutorial All rights reserved.

Follow Us

Facebook Twitter LinkedIn Printerest Reddit

Announcement

Its a big achivement for us, We make a partnership with TutorialWithExample.com for the better content of our users.

Still Need Help ?

Let us now about your issue and a Professional will reach you out.