Variables in PHP (Rules, Scope & Examples)
In this lesson: Declare PHP variables with $, naming rules, variable scope, global and static — examples with output in Hindi & English.
What is a variable
A variable is a named box in the computer's memory where you store a value so you can use it again later. In PHP every variable name starts with a dollar sign $. You put a value in with the = sign, and from then on the variable holds that value until you change it.
The special thing about PHP is that it is loosely typed — you never say whether a value is a number, text or true/false. You just assign it, and PHP works out the type on its own. This makes PHP quick to write and beginner-friendly.
$name) and whatever you drop inside is the value.Declaring variables
To create a variable, write $, then a name, then =, then the value. Here are a few of each common kind:
<?php
$name = "Aman"; // text (string)
$age = 18; // whole number (integer)
$price = 99.5; // decimal number (float)
$pass = true; // true/false (boolean)
echo "$name is $age years old";
?>
Notice we did not tell PHP that $age is a number or $name is text — PHP figured that out from the value we assigned. Also, inside double quotes PHP replaces $name and $age with their values automatically.
Values a variable can hold
| Type | Example | Meaning |
|---|---|---|
| String | "Aman" | Text inside quotes |
| Integer | 18 | Whole number |
| Float | 99.5 | Number with a decimal |
| Boolean | true / false | Yes/no value |
| NULL | null | No value at all |
Because PHP is loosely typed, the same variable can change type. $x = 5; then later $x = "hello"; is perfectly allowed — though for clean code it is better to keep one kind of value in one variable.
Naming rules
- Must start with
$followed by a letter or an underscore — never a number. - After that, letters, numbers and underscores are allowed (
$user_1is fine,$1useris not). - No spaces and no special characters like
-or@. - Case-sensitive:
$nameand$Nameare two different variables. - Use meaningful names —
$total_marksis far clearer than$t.
$x = 5), while == compares two values ($x == 5). Do not mix them up.Variable scope
Scope means where a variable can be seen. A variable made inside a function is local — it does not exist outside that function. A variable made outside is global.
<?php
$site = "CodingEasily"; // global
function show() {
echo $site; // ERROR: not visible here
}
function showRight() {
global $site; // now it is visible
echo $site;
}
showRight();
?>
By default a function cannot see outside variables. To use one, either pass it in as a parameter (the clean way) or bring it in with the global keyword.
Common mistakes
- Forgetting the
$. Writingname = "Aman"instead of$name = "Aman"causes an error. - Case mismatch. Setting
$Namebut reading$namegives an undefined-variable warning. - Using
=in a condition.if ($x = 5)assigns instead of comparing — use==. - Using a variable before assigning it. Always give a starting value first.
Summary
- Every PHP variable starts with
$and stores a value with=. - PHP is loosely typed — you never declare the type; PHP decides it.
- Names are case-sensitive and must start with a letter or underscore.
=assigns,==compares — keep them separate.- Function variables are local unless passed in or marked
global.
Next, learn what kinds of values exist in detail in PHP data types, or make decisions with if-else in PHP.
Frequently Asked Questions
Why do PHP variables start with a dollar sign?
$ sign tells PHP that the word after it is a variable, not a keyword or function. So $name is a variable but name is not. Every PHP variable, without exception, begins with $.Do I need to declare the type of a PHP variable?
Are PHP variable names case-sensitive?
$name, $Name and $NAME are three different variables. This is a very common source of bugs, so pick one style (usually lowercase) and stay consistent.What is the difference between = and == in PHP?
= assigns a value to a variable ($x = 5). A double == compares two values and returns true or false ($x == 5). Mixing them up is one of the most common beginner mistakes.