PHP Variables

Variables are containers for storing data (values). In PHP, a variable begins with a $ sign, followed by the name of the variable. PHP is dynamically typed: the type is associated with the value, not the variable name.

Declaring & Assigning

<?php
  $name = "Sonu";
  $age  = 25;
  echo "I am $name and $age years old.";
?>

Naming Rules

StartMust start with a letter or underscore (_)
AllowedLetters, numbers, underscores (A–Z, a–z, 0–9, _)
CaseCase-sensitive ($Name$name)
Examples$user, $_total, $age2
Bad$2age, $first-name, $user name

Inspecting Values with var_dump()

var_dump() prints type and value — very useful during learning.

<?php
  $x = 42;
  $y = "42";
  var_dump($x); // int(42)
  var_dump($y); // string(2) "42"
?>

Checking Variables: isset() & empty()

  • isset($v) → true if variable is defined and not NULL.
  • empty($v) → true for "", 0, "0", NULL, false, [], or undefined.
<?php
  $a = "";
  var_dump(isset($a));  // true
  var_dump(empty($a));  // true
  unset($a);
  var_dump(isset($a));  // false
?>

Data Types (Overview)

TypeExamplesNotes
NULL$x = NULL;No value
Booleantrue, falseLogical flags
Integer1, -5, 0xFFWhole numbers
Float3.14, 1.0e3Decimals / scientific
String"Hello", 'World'Text data
Array[1,2], ["a"=>1]Ordered maps
Objectnew stdClass()Instances of classes
ResourceDB connections, streamsExternal handles

Type Juggling & Casting

PHP automatically converts types when needed, but you can cast explicitly.

<?php
  $n = "25";
  $sum = (int)$n + 5;   // 30
  $f   = (float)"3.14";
  var_dump($sum, $f);
?>

String Interpolation

Variables inside double quotes are parsed; inside single quotes are not.

<?php
  $name = "Sonu";
  echo "Hello $name"; // Hello Sonu
  echo 'Hello $name'; // Hello $name
?>

Arrays (Quick Preview)

KindExample
Indexed$a = [10, 20, 30];
Associative$u = ["name"=>"Sonu", "age"=>25];
<?php
  $u = ["name"=>"Sonu", "age"=>25];
  echo "Name: " . $u["name"] . ", Age: " . $u["age"];
?>

Variable Scope

Variables declared inside a function are local. Use global or $GLOBALS to access globals, and static to persist between calls.

Local vs Global

<?php
  $g = 10;

  function test1() {
    // echo $g; // Undefined inside without global
  }

  function test2() {
    global $g;
    echo $g;
  }

  function test3() {
    echo $GLOBALS["g"];
  }
?>

static Variables

<?php
  function counter() {
    static $i = 0;
    $i++;
    echo $i . " ";
  }
  counter(); counter(); counter(); // 1 2 3
?>

Superglobals (Overview)

SuperglobalPurpose
$_GETQuery string parameters
$_POSTForm data (POST)
$_REQUESTGET + POST + COOKIE (use carefully)
$_SERVERServer & request info
$_COOKIECookies
$_SESSIONSession data
$_FILESUploaded files
$_ENVEnvironment variables
$GLOBALSAll global variables

Variable Variables

Using the value of one variable as the name of another. Use sparingly.

<?php
  $x = "name";
  $$x = "Sonu";
  echo $name; // Sonu
?>

References (&)

Two variables point to the same value in memory.

<?php
  $a = 10;
  $b =& $a;
  $b = 20;
  echo $a; // 20
?>

NULL

NULL is a special type with only one value: NULL. It represents a variable with no value.

<?php
  $x = NULL;
  var_dump($x);
?>

Best Practices

  • Use meaningful names: $totalPrice, $userEmail.
  • Initialize variables before use.
  • Avoid variable variables unless absolutely needed.
  • Use strict_types and type hints in modern PHP (we’ll cover later).

Practice Tasks

  1. Create variables for name, age, and city. Print a sentence using double-quoted interpolation.
  2. Use var_dump() to inspect values of different types (int, string, float, array).
  3. Write a function with a static counter and call it 5 times.
  4. Read a value from $_GET['color'], safely echo it using htmlspecialchars().
Next: Deep dive into Data Types (booleans, strings, arrays, objects, and conversions).