PHP Reference Β· Variables and Constants
PHP variables
Reference for PHP variables: the $ prefix, naming rules, assignment by value and by reference, undefined variables, interpolation and the unset state.
A variable in PHP is a name that starts with $ and holds a value. You do not declare it or give it a type: the first assignment creates it, and the same variable can hold an integer now and a string later. The type belongs to the value, not to the name.
Variable names are case-sensitive ($name and $Name are different), must start with a letter or underscore, and can then contain letters, digits and underscores. Letters include bytes 128β255, so $grΓΆΓe is legal, though ASCII names are the convention. $this is reserved. Function and class names, in contrast, are case-insensitive.
$price = 19.99; $price = "now a string"; $greeting = "Hello, {$price}!";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
$name = expression; // assign by value $alias = &$name; // assign by reference $$nameOfVariable = 1; // variable variable unset($name); // destroy isset($name); // set and not null
| Aspect | Rule |
|---|---|
| Name pattern | [a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]* after the $. |
| Case | Variable names are case-sensitive; keywords, functions and class names are not. |
| Scalars and arrays | Assigned by value. Arrays copy on write. |
| Objects | The variable holds a handle, so copies share one object. |
| Undefined read | Warning: Undefined variable $x, value null. |
| Interpolation | Only inside double quotes and heredoc: "$x", "{$x['a']}", "$a[0]", "$o->p". |
Usage notes
- Reading an undefined variable is a warning (since 8.0; it was a notice before), not an error. Writing to an undefined array element creates it silently.
- Interpolation limits.
"$a[key]"works with an unquoted key,"$o->a->b"only takes the first property, and complex expressions need braces:"{$o->a->b}". The"${name}"form is deprecated since PHP 8.2; use"{$name}". - Chained assignment
$a = $b = 0assigns right to left, and an assignment is an expression whose value is the assigned value, soif (($n = count($list)) > 0)works. - Names with other characters are possible with
${"my-var"}, but avoid them. - Superglobals (
$_GET,$GLOBALS, ...) are available in every scope. Normal variables are local to a function, see Variable scope. - Copies are cheap. PHP shares the data until one side changes (copy-on-write), so do not fear passing large arrays or strings around.
- References with
&make two names point at one value; see References. unset()removes the name; assigningnullkeeps the name but with no value.isset()isfalsefor both.- Use meaningful, consistent names; most style guides (PSR-12) recommend
$camelCasefor variables and properties.