PHP Global variables / Superglobal variables
- Hello guys ! In today's tutorial we will learn about PHP superglobal variables.
- PHP superglobal variables are built in variables in PHP .There are many Global variables in PHP like ...
- $_REQUEST
- $_COOKIE
- $_SESSION
- $_GET
- $_POST
- $_SERVER
- $GLOBALS
- Multiple lines comment
$GLOBALS :
- This global variable in PHP is used for making any variable global .
PHP Example
Edit it yourself
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 10;
function sum()
{
$GLOBALS['z'] = $GLOBALS['x']+$GLOBALS['y'];
}
sum();
echo $z;
?>
</body>
</html>
Output :
Edit it yourself20
$_SERVER :
- $_SERVER is a variable that stores data like path , header and script locations etc , or we can say it provides information about server and it's execution enervironment .
- The values in the `$_SERVER` array are generated by the web server and are accessible within PHP scripts.
- - `$_SERVER['PHP_SELF']`: The filename of the currently executing script, relative to the document root.
- - `$_SERVER['SERVER_NAME']`: The name of the server host where the script is running.
- - `$_SERVER['REQUEST_METHOD']`: The HTTP request method used to access the script (e.g., "GET", "POST", "PUT").
- - `$_SERVER['QUERY_STRING']`: The query string portion of the URL, if any.
- - `$_SERVER['HTTP_USER_AGENT']`: The user agent string of the client's browser.
- - `$_SERVER['REMOTE_ADDR']`: The IP address of the client making the request.
PHP Example
Edit it yourself
<!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 :
Edit it yourself$_REQUEST :
- $_REQUEST in PHP is used for collecting data that is submitted by the user by get or post method ..
- In form when method is declared as GET or POST so when the data is submitted that data called in php by using $_REQUEST method ...
PHP Example
Edit it yourself
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<form method="get">
<table>
<tr>
<th>Username</th>
<td>
<input type="text" name="unm" placeholder="Enter username"/>
</td>
</tr>
<tr>
<th>Password</th>
<td>
<input type="password" name="pass" placeholder="enter password">
</td>
</tr>
<tr>
<th colspan="2">
<input type="submit" name="submit">
</th>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_REQUEST['submit']))
{
$unm=$_REQUEST['unm'];
$pass=$_REQUEST['pass'];
echo $unm."<br>".$pass;
}
?>
Output :
Edit it yourself
hello
$_POST : :
- $_POST is a superglobal variable it is used to pass data when you click submit in any form .
- In post method there's no limit of passing data . Post method is safe for passsing data like passwords or any personal informations or any kind of private data .
PHP Example
Edit it yourself
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>POST</title>
</head>
<body>
<form method="post">
<input type="text" name="name" placeholder="Enter name :"/>
<input type="submit" name="submit">
</form>
<?php
if(isset($_REQUEST['submit']))
{
$nm = $_POST['name'];
echo $nm;
}
?>
</body>
</html>
<?php
if(isset($_REQUEST['submit']))
{
$unm=$_REQUEST['unm'];
$pass=$_REQUEST['pass'];
echo $unm."<br>".$pass;
}
?>
Output :
Edit it yourself
hello