A Beginner's Guide To PHP Variables
* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.
If you’re just starting with PHP, one of the first concepts you’ll need to master is variables.
Variables allow you to store and manipulate data, making your code dynamic and functional.
Whether you’re coming from another programming language or learning to code for the first time, this guide will walk you through PHP variables, how to define them, assign values, and best practices for using them effectively.
What Are PHP Variables?
In PHP, a variable is a named container that stores data. Unlike some other languages, PHP variables do not require explicit data type declarations—the language automatically determines the type based on the assigned value.
PHP Variable Syntax:
- All variables in PHP start with a dollar sign ($).
- Variable names must begin with a letter or underscore (_), followed by letters, numbers, or underscores.
- Variable names are case-sensitive (
$name
and$Name
are different variables).
Example:
<?php
$name = "John"; // String
$age = 25; // Integer
$price = 9.99; // Float
$isStudent = true; // Boolean
echo $name; // Output: John
?>
Assigning Values to Variables
Basic Assignment
You can assign values to variables using the assignment operator (=):
<?php
$city = "New York";
echo $city; // Output: New York
?>
Variable Reassignment
Variables can be reassigned at any time:
<?php
$number = 10;
$number = 20;
echo $number; // Output: 20
?>
Assigning Variables by Reference
You can assign variables by reference using the & symbol, meaning changes to one variable affect the other:
<?php
$a = 5;
$b = &$a; // $b references $a
$a = 10;
echo $b; // Output: 10
?>
PHP Variable Types
PHP supports multiple data types, which are assigned dynamically:
Data Type | Example |
---|---|
String | $text = "Hello"; |
Integer | $num = 100; |
Float | $price = 5.99; |
Boolean | $isLoggedIn = true; |
Array | $colors = ["red", "green", "blue"]; |
Object | $car = new Car(); |
NULL | $value = NULL; |
Variable Scope in PHP
The scope of a variable determines where it can be accessed. PHP has four main variable scopes:
1. Local Variables (Declared inside a function, accessible only within it)
<?php
function myFunction() {
$localVar = "I'm local";
echo $localVar;
}
myFunction(); // Output: I'm local
// echo $localVar; // Error: Undefined variable
?>
2. Global Variables (Declared outside a function, accessible inside functions using global
keyword)
<?php
$globalVar = "I'm global";
function showVar() {
global $globalVar;
echo $globalVar;
}
showVar(); // Output: I'm global
?>
3. Static Variables (Preserve their values between function calls)
<?php
function counter() {
static $count = 0;
$count++;
echo $count;
}
counter(); // Output: 1
counter(); // Output: 2
?>
4. Superglobals (Built-in global variables like $_GET
, $_POST
, $_SESSION
)
<?php
echo $_SERVER["PHP_SELF"]; // Outputs current script filename
?>
Best Practices for Using PHP Variables
- Use meaningful variable names (
$price
instead of$p
). - Follow naming conventions (camelCase or snake_case:
$userName
or$user_name
). - Initialize variables before use to avoid unexpected errors.
- Use constants (
define()
orconst
) for values that shouldn’t change.
Example of Constants:
<?php
define("SITE_NAME", "MyWebsite");
echo SITE_NAME; // Output: MyWebsite
?>
Conclusion
PHP variables are the foundation of dynamic programming, allowing you to store, manipulate, and retrieve data easily. Understanding variable assignment, scope, and best practices will help you write clean and efficient PHP code.