A PHP variable stores a value you can use and change later — the same idea you met in Intro to Programming, with PHP's own syntax on top.
Every PHP variable name starts with a dollar sign ($). This isn't optional decoration — it's how PHP tells a variable apart from a function name, a constant, or a keyword.
<?php
$name = "Amit";
$age = 21;
echo "$name is $age years old.";
?>Notice that variables can be dropped directly inside a double-quoted string and PHP will substitute their value — this is called string interpolation, and it only works with double quotes, not single quotes (you'll look at strings properly in an upcoming lesson).
| Rule | Example |
|---|---|
| Must start with a letter or underscore (never a number) | $_id — valid, $1id — invalid |
| Can contain letters, numbers, and underscores after that | $user_2 — valid |
| No spaces or hyphens allowed | $user name — invalid |
| Case-sensitive | $Name and $name are different variables |
You never declare a variable's type in PHP — no int, string, or similar keyword up front. PHP figures out the type from the value you assign, and a variable can hold a completely different type later if you reassign it:
<?php
$value = 10; // an integer
$value = "ten"; // now a string — perfectly legal
?>This flexibility is convenient, but it's also a common source of subtle bugs — the next lesson covers PHP's data types properly, including exactly how PHP decides what a value "really" is when it needs to.
No separate declaration step
PHP variables don't need to be declared before use the way some languages require — assigning a value to $whatever for the first time is enough to create it. There's no separate "declaration" step.