Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 04

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.

💡 Think of a variable as a labelled jar: the label is the name ($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";
?>
Aman is 18 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

TypeExampleMeaning
String"Aman"Text inside quotes
Integer18Whole number
Float99.5Number with a decimal
Booleantrue / falseYes/no value
NULLnullNo 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_1 is fine, $1user is not).
  • No spaces and no special characters like - or @.
  • Case-sensitive: $name and $Name are two different variables.
  • Use meaningful names — $total_marks is far clearer than $t.
⚠️ = assigns a value ($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();
?>
CodingEasily

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 $. Writing name = "Aman" instead of $name = "Aman" causes an error.
  • Case mismatch. Setting $Name but reading $name gives 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?
The $ 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?
No. PHP is loosely typed, which means you never write the type. You just assign a value and PHP decides the type automatically — a number, text, true/false and so on. The same variable can even hold a number now and text later.
Are PHP variable names case-sensitive?
Yes. $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?
A single = 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.
What happens if I use a variable before assigning it a value?
PHP treats an unassigned variable as NULL and usually shows a warning (Undefined variable). Always assign a starting value before you use a variable, especially inside conditions and loops.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicData Types in PHP Your progress: 4 / 50 (8%) · Lesson 5 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free