Variables and Constants in PHP
Understand how to declare and use variables with the $ prefix, define constants with define() and const, and work with variable scope in PHP.
Introduction to Variables
A variable is a named container that holds a value in memory. In PHP, every variable starts with a `$` sign followed by its name. PHP is dynamically typed — you do not declare a type upfront; PHP figures it out from the value you assign.
1. What is a Variable?
Variables store data in memory so it can be reused and changed throughout a program.
2. Dynamic Typing
PHP decides the type from the assigned value — you can even change the type of a variable by reassigning it.
Declaring Variables
PHP variables are created the moment you assign a value to them — there is no separate declaration step. The only rules are: the name must start with `$`, followed by a letter or underscore, and then any combination of letters, numbers, or underscores.
1. Naming Rules
Variable names are case-sensitive and must follow a specific pattern.
2. Multiple Assignment
Assign the same value to multiple variables in one statement.
3. Null and Unsetting Variables
A variable can hold `null`, and `unset()` removes it from memory entirely.
Variable Scope
PHP has three main scopes: **local** (inside a function), **global** (outside all functions), and **static** (persists between function calls). Unlike many languages, PHP functions do NOT automatically see global variables — you must explicitly declare them with the `global` keyword.
1. Local Scope
Variables declared inside a function exist only within that function.
2. Global Scope and the global Keyword
Use the `global` keyword to read or modify a global variable from inside a function.
3. Static Variables
A `static` variable retains its value between multiple calls to the same function.
Constants
Constants are named values that cannot be changed once defined. Unlike variables, constants do not start with `$`. PHP offers two ways to define them: `define()` (at runtime) and `const` (at compile time, for class or file-level constants).
1. define() — Runtime Constants
`define()` creates a constant at runtime and can be called anywhere in a script.
2. const — Compile-time Constants
`const` defines a constant at compile time — it must appear at the top level or inside a class.
3. Class Constants
Constants can be attached to a class and accessed via the `::` (scope resolution) operator.
4. PHP Predefined Constants
PHP ships with many built-in constants for file paths, versions, OS info, and more.
Variable Variables
PHP has a unique feature called "variable variables" — `$$var` uses the value of `$var` as a variable name. While clever, this should be used sparingly as it makes code hard to follow.
1. Variable Variables Basics
Prefix a variable with an extra `$` to treat its value as another variable name.
Best Practices
Good variable naming and scoping habits make PHP code easier to read, test, and maintain.
1. Naming Conventions
Follow community-standard naming patterns for variables and constants.
2. Prefer const Over define()
Use `const` for file/class-level constants and `define()` only when you need runtime flexibility.