PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Predefined Variables

PHP provides several predefined variables that are always available to a script. These variables are often referred to as superglobals, because they are available in all scopes throughout a script. Here are some of the most commonly used ones:

  • $_GET: An associative array of variables passed to the current script via the URL parameters.
// URL: www.example.com/test.php?name=John
echo $_GET["name"]; // Outputs: John
  • $_POST: An associative array of variables passed to the current script via the HTTP POST method.
// Assuming a form submitted "name" as "John"
echo $_POST["name"]; // Outputs: John
  • $_FILES: An associative array of items uploaded to the current script via the HTTP POST method.
echo $_FILES['userfile']['name']; // Outputs the name of the uploaded file
  • $_COOKIE: An associative array of variables passed to the current script via HTTP cookies.
echo $_COOKIE["user"]; // Outputs the value of the cookie named "user"
  • $_SESSION: An associative array containing session variables available to the current script.
echo $_SESSION["username"]; // Outputs the value of the session variable "username"
  • $_SERVER: An associative array containing information such as headers, paths, and script locations.
echo $_SERVER['HTTP_HOST']; // Outputs the Host header from the current request
  • $_REQUEST: An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
echo $_REQUEST["name"]; // Outputs the value of "name" from either $_POST, $_GET or $_COOKIE
  • $_ENV: An associative array of variables passed to the current script via the environment method.
echo $_ENV["PATH"]; // Outputs the value of the environment variable "PATH"
  • $GLOBALS: An associative array containing references to all variables which are currently defined in the global scope of the script.
$x = 75;
$y = 25;

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

addition();
echo $z; // Outputs: 100

Remember that these predefined variables are always available, regardless of the scope of your script.

  1. $_GET, $_POST, and $_REQUEST in PHP:

    • $_GET is used to collect form data after submitting an HTML form with the method="get" attribute. $_POST is used with method="post". $_REQUEST contains data from both $_GET and $_POST.
    // Example using $_GET
    $username = $_GET['username'];
    
    // Example using $_POST
    $password = $_POST['password'];
    
    // Example using $_REQUEST
    $email = $_REQUEST['email'];
    
  2. Working with $_SESSION and $_COOKIE variables in PHP:

    • $_SESSION is used to store session variables, and $_COOKIE holds cookie variables.
    // Using $_SESSION
    session_start();
    $_SESSION['user_id'] = 123;
    
    // Using $_COOKIE
    setcookie('user_language', 'en', time() + (86400 * 30), "/");
    
  3. Server-related variables like $_SERVER in PHP:

    • $_SERVER contains information about the server and the execution environment.
    $serverName = $_SERVER['SERVER_NAME'];
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    
  4. Using $_FILES for file upload handling in PHP:

    • $_FILES is used to access information about uploaded files.
    $uploadedFile = $_FILES['file'];
    $fileName = $uploadedFile['name'];
    $fileTemp = $uploadedFile['tmp_name'];
    
  5. Accessing environment variables with $_ENV in PHP:

    • $_ENV holds environment variables.
    $databaseUser = $_ENV['DB_USER'];
    $databasePassword = $_ENV['DB_PASSWORD'];
    
  6. PHP magic constants and their role as predefined variables:

    • Magic constants are predefined constants in PHP. Some include __FILE__, __LINE__, __DIR__, __FUNCTION__, __CLASS__, and __NAMESPACE__.
    echo "This script is in: " . __FILE__;
    echo "This function is: " . __FUNCTION__;