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
| Start | Must start with a letter or underscore (_) |
|---|---|
| Allowed | Letters, numbers, underscores (A–Z, a–z, 0–9, _) |
| Case | Case-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 notNULL.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)
| Type | Examples | Notes |
|---|---|---|
| NULL | $x = NULL; | No value |
| Boolean | true, false | Logical flags |
| Integer | 1, -5, 0xFF | Whole numbers |
| Float | 3.14, 1.0e3 | Decimals / scientific |
| String | "Hello", 'World' | Text data |
| Array | [1,2], ["a"=>1] | Ordered maps |
| Object | new stdClass() | Instances of classes |
| Resource | DB connections, streams | External 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)
| Kind | Example |
|---|---|
| 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)
| Superglobal | Purpose |
|---|---|
$_GET | Query string parameters |
$_POST | Form data (POST) |
$_REQUEST | GET + POST + COOKIE (use carefully) |
$_SERVER | Server & request info |
$_COOKIE | Cookies |
$_SESSION | Session data |
$_FILES | Uploaded files |
$_ENV | Environment variables |
$GLOBALS | All 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_typesand type hints in modern PHP (we’ll cover later).
Practice Tasks
- Create variables for name, age, and city. Print a sentence using double-quoted interpolation.
- Use
var_dump()to inspect values of different types (int, string, float, array). - Write a function with a
staticcounter and call it 5 times. - Read a value from
$_GET['color'], safely echo it usinghtmlspecialchars().