PHPVariables

Variables

A variable in PHP is a named container for a value that can change while your script runs. Unlike languages such as Java or C#, PHP variables are not declared with a type ahead of time — you simply assign a value, and PHP figures out the type for you. Every variable name starts with a dollar sign ($), which is what tells the parser "this is a variable" rather than a bare word or function name.

Declaring and assigning

There is no separate "declaration" step in PHP — assignment is declaration. The first time you write to $name, that variable comes into existence.

Basic assignment

PHP
<?php
$name = "Aisha";
$age = 29;
$isActive = true;
$price = 19.99;

echo "{$name} is {$age} years old.";
Aisha is 29 years old.
Dynamic typing

A variable's type is determined by whatever value it currently holds, and that type can change over the variable's lifetime. This is called dynamic typing. It is convenient for quick scripts but can hide bugs in larger codebases if you are not deliberate about it.

The same variable, different types

PHP
<?php
$value = 10;        // int
var_dump($value);

$value = "10";       // now a string
var_dump($value);

$value = 10.5;       // now a float
var_dump($value);
int(10)
string(2) "10"
float(10.5)
Naming rules
  • A variable name must start with a letter or underscore, never a digit: $_count is valid, $1count is not.

  • After the first character, letters, digits, and underscores are all allowed: $user_2 is valid.

  • PHP variable names are case-sensitive: $total and $Total are two different variables.

  • There is no fixed length limit in practice, but short, meaningful names read better than cryptic abbreviations.

Valid vs invalid names

PHP
<?php
$user_name = "ok";     // valid
$_hidden   = "ok";     // valid, starts with underscore
$Price2    = "ok";     // valid, digit is fine after first char

// $2fast   = "no";    // invalid: cannot start with a digit
// $user-name = "no";  // invalid: hyphen is not allowed in a name
Variable interpolation in strings

Double-quoted strings and heredocs will substitute variables directly; single-quoted strings will not. The modern, unambiguous way to embed a variable (especially one followed by letters, or a property/array access) is curly-brace interpolation: "{$variable}".

Single vs double quotes vs curly interpolation

PHP
<?php
$user = ["name" => "Marco"];
$count = 3;

echo 'Hello $user[name]';        // literal text, no interpolation
echo "\n";
echo "Hello {$user['name']}";     // interpolated: Hello Marco
echo "\n";
echo "You have {$count}apples";  // curly braces avoid ambiguity
Hello $user[name]
Hello Marco
You have 3apples
Ambiguous interpolation without braces
Writing `"$countapples"` does **not** print `3apples` — PHP looks for a variable literally named `$countapples`, finds nothing, and prints an empty string (with a warning in PHP 8). Always wrap the variable in curly braces when it is immediately followed by identifier characters.
isset(), unset(), and empty()

Three functions come up constantly when working with variables that might not exist yet: isset() checks whether a variable is set and is not null; unset() destroys a variable entirely; empty() checks whether a variable is either unset or holds a "falsy" value (covered in depth on the Booleans page).

isset, unset, and empty in action

PHP
<?php
$city = "Toronto";

var_dump(isset($city));   // bool(true)
var_dump(isset($country)); // bool(false) - never assigned

unset($city);
var_dump(isset($city));   // bool(false) - destroyed

$votes = 0;
var_dump(isset($votes));  // bool(true)  - it exists
var_dump(empty($votes));  // bool(true)  - but 0 counts as empty
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
Why isset() matters for arrays and superglobals
`isset()` is the standard guard before reading `$_GET`, `$_POST`, or array keys that might not be present, because accessing an undefined array key directly raises a warning in modern PHP. Writing `isset($_GET['page']) ? $_GET['page'] : 1` (or the shorter `??` operator) avoids that warning entirely.
Tip
Prefer the null coalescing operator `??` over `isset()` ternaries for default values: `$page = $_GET['page'] ?? 1;` is shorter, reads better, and does exactly what `isset($_GET['page']) ? $_GET['page'] : 1` does.