PHP Variables


Variables in PHP are used to store data that can be manipulated and retrieved throughout a script. They are a fundamental part of PHP programming, enabling developers to handle dynamic content and interact with users, databases, and other parts of an application. Here’s a detailed explanation of variables in PHP:

1. Declaring Variables

In PHP, variables are declared using a dollar sign ($) followed by the variable name. The variable name must start with a letter or an underscore, followed by any number of letters, numbers, or underscores.

  • Syntax:

    $variableName = value;
  • Example:

    <?php $name = "John Doe"; // String variable $age = 30; // Integer variable $is_admin = true; // Boolean variable ?>

    In this example:

    • $name is a variable that holds the string "John Doe".
    • $age is a variable that holds the integer 30.
    • $is_admin is a variable that holds the boolean value true.

2. Variable Naming Rules

  • Start with a Letter or Underscore: Variable names must begin with a letter (a–z, A–Z) or an underscore (_).
  • Case-Sensitive: Variable names in PHP are case-sensitive, meaning $Variable and $variable are treated as two different variables.
    <?php $Variable = "Uppercase V"; $variable = "Lowercase v"; echo $Variable; // Outputs: Uppercase V echo $variable; // Outputs: Lowercase v ?>
  • No Special Characters or Spaces: Variable names cannot contain spaces or special characters, except for the underscore (_).
  • Descriptive Naming: It's good practice to use descriptive names for variables to make the code more readable.

3. Assigning Values to Variables

PHP allows you to assign different types of values to variables, including strings, integers, floats, booleans, arrays, and objects.

  • String:

    <?php $greeting = "Hello, World!"; ?>
  • Integer:

    <?php $year = 2024; ?>
  • Float (Double):

    <?php $price = 19.99; ?>
  • Boolean:

    <?php $is_valid = true; ?>
  • Array:

    <?php $colors = array("red", "green", "blue"); ?>
  • Object:

    <?php class Car { public $model; public $color; public function __construct($model, $color) { $this->model = $model; $this->color = $color; } } $myCar = new Car("Toyota", "Red"); ?>

4. Variable Scope

The scope of a variable in PHP refers to the context within which it is defined and accessible.

  • Global Scope:

    • A variable declared outside of a function has a global scope and can be accessed anywhere outside functions.
    • To access a global variable inside a function, you can use the global keyword or the $GLOBALS array.
    <?php $globalVar = "I'm global!"; function testFunction() { global $globalVar; echo $globalVar; // Outputs: I'm global! } testFunction(); ?>
  • Local Scope:

    • A variable declared within a function is local to that function and cannot be accessed outside of it.
    <?php function testFunction() { $localVar = "I'm local!"; echo $localVar; } testFunction(); // Outputs: I'm local! echo $localVar; // Error: Undefined variable ?>
  • Static Scope:

    • A static variable exists only in the local function scope but does not lose its value when the function exits.
    <?php function testStatic() { static $counter = 0; $counter++; echo $counter; } testStatic(); // Outputs: 1 testStatic(); // Outputs: 2 testStatic(); // Outputs: 3 ?>

5. Variable Variables

PHP allows you to use the value of a variable as the name of another variable.

  • Example:

    <?php $varName = "message"; $$varName = "Hello, World!"; // Equivalent to $message = "Hello, World!"; echo $message; // Outputs: Hello, World! ?>

    In this example:

    • $$varName becomes $message because the value of $varName is "message".

6. Predefined Variables (Superglobals)

PHP provides several built-in variables that are always accessible, regardless of scope. These are known as superglobals.

  • Examples:
    • $_GET, $_POST: Used to collect form data.
    • $_SESSION: Used to store session variables.
    • $_COOKIE: Used to store cookies.
    • $_SERVER: Contains information about headers, paths, and script locations.

7. Unset Variables

You can unset a variable using the unset() function, which destroys the specified variable.

  • Example:
    <?php $var = "Hello, World!"; unset($var); echo $var; // Outputs nothing as $var is now undefined ?>