Variables
Learn how to declare and use variables in PHP, the naming rules that apply to them, and how PHP's dynamic typing works.
Introduction
Variables are the containers your program uses to hold information as it runs — a user's name, a product price, the result of a calculation. Almost nothing useful in PHP happens without them.
In this lesson, you will learn how PHP variables are declared, the rules that govern valid variable names, how PHP's dynamic typing lets a variable hold any kind of value, and a rarely-used but interesting feature called variable variables.
- How to create a variable using the $ prefix.
- Why PHP has no separate "declaration" step — assignment does it all.
- The naming rules every variable must follow.
- What dynamic typing means in practice.
- A quick look at variable variables ($$name).
Declaring Variables
Every PHP variable starts with a dollar sign ($), followed by the variable's name. Unlike some languages, PHP has no separate step for declaring a variable before using it — simply assigning a value to a $name creates that variable on the spot.
<?php
$name = "Ananya";
$age = 22;
$isStudent = true;
echo "$name is $age years old.";
?>Ananya is 22 years old.There is no `var`, `let`, or `int` keyword needed before a variable name in PHP. The first time you assign a value to $something, that variable comes into existence with that value and type.
You can reassign a variable at any time, and even change what type of value it holds.
<?php
$data = "hello";
echo $data . "\n";
$data = 42;
echo $data . "\n";
?>hello
42Naming Rules
PHP variable names must follow a specific pattern. Breaking these rules causes a parse error before your script even runs.
Starts With $
Every variable name begins with a dollar sign, e.g. $total.
First Character
After the $, the name must start with a letter or an underscore — never a digit.
Remaining Characters
After the first character, letters, digits, and underscores are all allowed.
Case-Sensitive
$total, $Total, and $TOTAL are three completely different variables.
<?php
$user_name = "valid"; // valid: letters and underscore
$_count = 10; // valid: starts with underscore
$price2 = 99.99; // valid: digit is fine after first char
// $2price = 5; // INVALID: cannot start with a digit
// $user-name = "x"; // INVALID: hyphens are not allowed
echo $user_name . " " . $_count . " " . $price2;
?>valid 10 99.99Because $Price and $price are different variables, a simple typo in capitalization can silently create a brand new, empty variable instead of using the one you meant — often producing warnings rather than obvious errors.
Dynamic Typing
PHP is dynamically typed: you never declare what type of data a variable will hold, and a variable's type can change freely as your script runs, simply by assigning it a different kind of value.
<?php
$value = "twenty-five"; // starts as a string
echo gettype($value) . "\n";
$value = 25; // now an integer
echo gettype($value) . "\n";
$value = 25.5; // now a float
echo gettype($value) . "\n";
$value = true; // now a boolean
echo gettype($value) . "\n";
?>string
integer
double
booleanDynamic typing makes PHP fast to write, but reusing one variable for unrelated purposes across a script makes code harder to follow. Most style guides recommend keeping a variable's type consistent throughout its lifetime, even though PHP does not require it.
Variable Variables
PHP has an unusual, rarely-used feature called a variable variable, written with two dollar signs: $$name. It lets the value of one variable be used as the name of another variable.
<?php
$fruit = "apple";
$$fruit = "red"; // this creates a new variable called $apple
echo $fruit . "\n"; // apple
echo $apple . "\n"; // red (created dynamically!)
?>apple
redVariable variables can make code very difficult to read and debug, since the actual variable name being used is only known at runtime. They show up occasionally in older PHP codebases or specific templating tricks, but arrays and associative arrays are almost always a clearer solution.
Common Mistakes
- Forgetting the $ prefix when referring to a variable.
- Mixing up variable names by case, e.g. $userName vs $username.
- Starting a variable name with a digit, which causes a parse error.
- Overusing variable variables ($$name) when a normal array would be far clearer.
Best Practices
- Choose descriptive variable names like $totalPrice instead of $tp.
- Stick to one consistent naming style, such as camelCase, throughout a project.
- Avoid changing a variable's type midway through a script unless there is a clear reason to.
- Reach for arrays instead of variable variables when you need dynamically named data.
Frequently Asked Questions
Do I need to declare a variable's type in PHP?
No. PHP is dynamically typed — a variable's type is determined automatically by the value you assign to it.
Can a variable name contain a space or hyphen?
No. After the $, only letters, digits, and underscores are allowed, and the name cannot start with a digit.
Are $Total and $total the same variable?
No. PHP variable names are case-sensitive, so these are two entirely separate variables.
What is a variable variable used for?
It lets you use the value of one variable as the name of another ($$name). It is an advanced, rarely used feature — arrays are usually a clearer choice.
What happens if I use a variable before assigning it a value?
PHP will raise a warning ("undefined variable") and treat its value as null, but it will not stop the script from running.
Key Takeaways
- Variables are declared with a $ prefix; there is no separate declaration step.
- Assigning a value to a new $name creates that variable immediately.
- Names must start with a letter or underscore, followed by letters, digits, or underscores, and are case-sensitive.
- PHP is dynamically typed — a variable's type is determined by its current value and can change.
- Variable variables ($$name) exist but are an advanced, rarely-used feature.
Summary
Variables in PHP are simple to create — just prefix a name with $ and assign it a value. Names follow clear rules and are case-sensitive, and PHP's dynamic typing means a variable can hold, and later change to, any type of value.
Next, you will look at constants — values that, once set, are never meant to change for the lifetime of the script.
- You can declare and reassign variables using $.
- You know the naming rules for valid variable names.
- You understand PHP's dynamic typing.
- You have seen how variable variables ($$name) work.