PHP — Variables
The $ prefix
Every variable in PHP starts with a $ sign:
<?php
$name = "Alice"; // string
$age = 30; // integer
$price = 19.99; // float
$isActive = true; // boolean
echo $name; // Alice
echo $age; // 30
?>
PHP determines the type at runtime based on the value assigned. You do not declare types explicitly.
Naming rules
- Must start with a letter or underscore
_ - Can only contain letters, numbers, and underscores
- Are case-sensitive (
$name≠$Name≠$NAME) - Cannot be a reserved keyword (e.g.,
$class,$functionare valid though — they shadow keywords when prefixed with$)
<?php
$valid_name = "OK";
$valid2 = "OK";
$_private = "OK";
$123 = "INVALID"; // Error: cannot start with a number
$my-name = "INVALID"; // Error: hyphens not allowed
?>
Dynamic typing
PHP is dynamically typed — a variable can change its type at any time:
<?php
$x = 10; // $x is an integer
$x = "ten"; // Now $x is a string
$x = 10.5; // Now $x is a float
$x = true; // Now $x is a boolean
echo gettype($x); // boolean
?>
Type checking
Use gettype() or the var_dump() function to inspect a variable's type:
<?php
$var = 3.14;
echo gettype($var); // double (float)
var_dump($var); // float(3.14)
?>
var_dump() outputs both the type and value — useful for debugging:
<?php
$name = "Alice";
$age = 30;
$price = 19.99;
var_dump($name); // string(5) "Alice"
var_dump($age); // int(30)
var_dump($price); // float(19.99)
?>
Scope
Variables have different visibility depending on where they are declared:
<?php
// Global scope
$globalVar = "I am global";
function myFunction() {
// Local scope — cannot access $globalVar here
$localVar = "I am local";
echo $localVar; // OK
echo $globalVar; // Notice: Undefined variable
}
?>
Use the global keyword to access global variables inside functions:
<?php
$counter = 0;
function increment() {
global $counter;
$counter++;
}
increment();
echo $counter; // 1
?>
Alternatively, use the $GLOBALS superglobal array:
<?php
$counter = 0;
function increment() {
$GLOBALS['counter']++;
}
increment();
echo $GLOBALS['counter']; // 1
?>
Variable variables
PHP supports variable variables — using the value of one variable as the name of another:
<?php
$varName = "greeting";
$$varName = "Hello, world!"; // Creates $greeting
echo $greeting; // Hello, world!
echo $$varName; // Hello, world!
?>
Use variable variables sparingly — they make code harder to follow and debug.
Reference assignment
PHP uses copy-on-assignment by default. To share the same value, use &:
<?php
$a = 10;
$b = $a; // $b gets a copy of $a's value
$b = 20;
echo $a; // 10 (unchanged)
$c = &$a; // $c references $a
$c = 20;
echo $a; // 20 (changed!)
?>
Superglobal variables
PHP provides built-in superglobal arrays available anywhere:
<?php
$_GET // URL parameters
$_POST // Form data submitted via POST
$_REQUEST // Combined GET, POST, and COOKIE
$_SERVER // Server and execution environment info
$_ENV // Environment variables
$_SESSION // Session variables
$_FILES // File upload information
$_COOKIE // Cookie data
$GLOBALS // All global variables
?>
<?php
// Example: accessing form data
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$username = $_POST["username"];
echo "Welcome, $username!";
}
?>
isset() and unset()
Check if a variable exists with isset(), and remove it with unset():
<?php
$var = "Hello";
echo isset($var); // 1 (true)
unset($var);
echo isset($var); // (empty — false)
var_dump($var); // NULL
?>
Summary
| Concept | Example |
|---|---|
| Declaration | $name = "Alice"; |
| Type check | gettype($var) or var_dump($var) |
| Scope | global $var; or $GLOBALS['var'] |
| Reference | $b = &$a; |
| Check existence | isset($var) |
| Remove | unset($var) |
Mini Practice
Write PHP code that:
- Declares variables of at least 4 different types
- Uses
var_dump()to print each variable's type and value - Creates a function that modifies a global variable using the
globalkeyword - Demonstrates reference assignment between two variables
Up Next
In the next lesson, you'll learn about Data Types — the different types of values PHP can handle.
Related Topics
Frequently Asked Questions about Variables
What is Variables in PHP?
Variables is a fundamental concept in PHP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Variables?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Variables.
Why is Variables important in PHP?
Variables is essential for PHP development. Understanding this concept will help you write better code and solve real-world problems more effectively.