Global Variable in PHP


Prev Tutorial Next Tutorial

Super Global Variable in PHP

Some pre-defined variables are available in php which is know as superglobals variable, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

List of superglobal variables are give below;

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

$GLOBALS variable

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). Using $GLOBALS[index] stores all global variables in an array. Here index hold all variable.

$GLOBALS Example

<?php
$x = 75; 
$y = 25;

function addition() { 
    $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}
 
addition(); 
echo $z; 

?>

$_SERVER Variable

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

$_SERVER 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'];

?>

$_REQUEST Variable

PHP $_REQUEST is used to collect data after submitting an HTML form.

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

$_POST Variable

PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

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

</body>
</html>

$_GET Variable

PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".

$_GET Example

<html>
<body>

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

</body>
</html>

$_FILES Variable

The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file name and errors associated with file.

$_FILES Example

<!DOCTYPE html>
<html>
<body>

<form action="uploader.php" method="post" enctype="multipart/form-data">
    Select File:  
    <input type="file" name="fileToUpload"/>
    <input type="submit" value="Upload Image" name="submit"/>
</form>

<?php

$target_path = "c:/";  
$target_path = $target_path.basename( $_FILES['fileToUpload']['name']);   
  
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) 
{  
    echo "File uploaded successfully!";  
}
else
{
  echo "Sorry, file not uploaded, please try again!";  
}  

?>

<body>
<html>

$_ENV Variable

$_ENV global variable in php is a predefined reserved variable that contains an array of information related to the environment in which php script is running.

$_ENV Example

<?php

echo 'My username is ' .$_ENV["USER"] . '!';

?>

$_COOKIE Variable

$_COOKIE is used to create cookie.

$_COOKIE Example

<?php
$cookie_name = "user";
$cookie_value = "Hitesh Kumar";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

<!DOCTYPE html>
<html>
<body>
<?php

if(!isset($_COOKIE[$cookie_name]))
{
 echo "Cookie named '" . $cookie_name . "' is not set!";
} else 
{
 echo "Cookie '" . $cookie_name . "' is set!
"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <body> <html>

$_SESSION Variable

$_SESSION Variable is used to create session.

$_SESSION Example

<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html>
<body>
<?php

// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";

?>
<body>
<html>

Prev Tutorial Next Tutorial

Advertisements
Download Projects