Superglobals are per-defined variables in PHP and they are always accessible.
The PHP superglobal variables are :
$_GLOBALS : It is used to access global variables from anywhere in the PHP script.
Example :
Output :
$_SERVER : $_SERVER holds information about headers, paths, and script locations.
Example :
Output :
The following table lists the most important elements that can be use inside $_SERVER :
$_REQUEST : It is used to collect data after submitting an HTML form.
Example :
Output :
The PHP superglobal variables are :
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
$_GLOBALS : It is used to access global variables from anywhere in the PHP script.
Example :
<?php
$x = 15;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo "Sum is : ".$z;
?>
$x = 15;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo "Sum is : ".$z;
?>
Output :
Sum is : 40
$_SERVER : $_SERVER holds information about headers, paths, and script locations.
Example :
<?php
echo "Server Address : ".$_SERVER['SERVER_ADDR'];
echo "<br>";
// Retrieves the client browser type, version number, library, and platform the browser is configured for.
echo $_SERVER['HTTP_USER_AGENT'];
?>
echo "Server Address : ".$_SERVER['SERVER_ADDR'];
echo "<br>";
// Retrieves the client browser type, version number, library, and platform the browser is configured for.
echo $_SERVER['HTTP_USER_AGENT'];
?>
Output :
Server Address : 127.0.0.1
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
The following table lists the most important elements that can be use inside $_SERVER :
Element | Description |
---|---|
$_SERVER['PHP_SELF'] | Returns the filename of the currently executing script |
$_SERVER['SERVER_ADDR'] | Returns the IP address of the host server |
$_SERVER['SERVER_NAME'] | Returns the name of the host server (such as www.neoogy.com) |
$_SERVER['SERVER_SOFTWARE'] | Returns the server identification string (such as Apache/2.4.39 (Win64) OpenSSL/1.1.1c PHP/7.3.7) |
$_SERVER['SERVER_PROTOCOL'] | Returns the name and revision of the information protocol (such as HTTP/1.1) |
$_SERVER['REQUEST_METHOD'] | Returns the request method used to access the page (such as POST)y |
$_SERVER['HTTP_ACCEPT'] | Returns the Accept header from the current request |
$_SERVER['HTTP_HOST'] | Returns the Host header from the current request |
$_SERVER['REMOTE_ADDR'] | Returns the IP address from where the user is viewing the current page |
$_SERVER['REMOTE_HOST'] | Returns the Host name from where the user is viewing the current page |
$_SERVER['SERVER_PORT'] | Returns the port on the server machine being used by the web server (such as 80) |
$_SERVER['SCRIPT_NAME'] | Returns the path of the current script |
$_SERVER['SCRIPT_URI'] | Returns the URI of the current page |
$_REQUEST : It is used to collect data after submitting an HTML form.
Example :
<!DOCTYPE html>
<html>
<body>
<form action="" method="post" style="text-align:center;">
<tr>
<label>Name</label>
<td><input type="text" name="fname" placeholder="Enter your Name"></td>
</tr><br />
<tr>
<input type="submit" name="submit" value="Submit">
</tr>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if(isset($_REQUEST['submit']))
{
echo "Welcome ".$name;
}
else{
echo"";
}
}
?>
</body>
</html>
<html>
<body>
<form action="" method="post" style="text-align:center;">
<tr>
<label>Name</label>
<td><input type="text" name="fname" placeholder="Enter your Name"></td>
</tr><br />
<tr>
<input type="submit" name="submit" value="Submit">
</tr>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if(isset($_REQUEST['submit']))
{
echo "Welcome ".$name;
}
else{
echo"";
}
}
?>
</body>
</html>
Output :
The remaining superglobals variable are not discussed in this tutorials.
0 Comments