← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 10 of 13

PHP Variables

A PHP variable is a labeled box for holding information, and its label always starts with a dollar sign. You can drop something in, look at it, or swap it for something new whenever you like.
Syntax
php
$variable_name = value;

echo $variable_name;
echo "Text with $variable_name inside";

Variable Declaration

Every PHP variable name starts with a dollar sign — $name, not name — which is how the interpreter tells a variable apart from a constant, function, or keyword at a glance.

You never declare a type up front; the variable simply starts existing the first time you assign it a value.

Example: Variable Declaration

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print `$name` to the output
echo $name;
?>

Dynamic Typing

Because PHP is dynamically typed, the same variable can hold a string at one point in your script and a number later on — PHP just reassigns it and moves on.

This flexibility is convenient for quick scripts, but it's also why type-related bugs can creep in silently in larger codebases.

Example: Dynamic Typing

php
<?php
// Declare `$value`, set to "Hello"
$value = "Hello";
// Print a detailed dump (with types) of `$value`
var_dump($value);
// Declare `$value`, set to `42`
$value = 42;
// Print a detailed dump (with types) of `$value`
var_dump($value);
?>

Outputting Variables

Double-quoted strings actively substitute a variable's current value wherever $variable appears inside them — this is called interpolation.

Single-quoted strings skip that step entirely and print the literal characters $variable, which is faster when you genuinely don't need substitution.

Example: Outputting Variables

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "Double-quoted: $name\n" to the output
echo "Double-quoted: $name\n";
// Print 'Single-quoted: $name' to the output
echo 'Single-quoted: $name';
?>

Variable Scope

A variable declared outside any function has global scope and is visible to top-level code throughout the script.

A variable declared inside a function is local to that function by default — it's created fresh on each call and disappears once the function returns, invisible to code outside it.

Example: Variable Scope

php
<?php
// Declare `$globalVar`, set to "I am global"
$globalVar = "I am global";

// Define the function `showScope` with no parameters
function showScope() {
    // Declare `$localVar`, set to "I am local"
    $localVar = "I am local";
    // Print `$localVar . "\n"` to the output
    echo $localVar . "\n";
}
// Call `showScope()`
showScope();
// Print `$globalVar` to the output
echo $globalVar;
?>

Variable Variables

PHP supports 'variable variables' using a double dollar sign: $$name uses the *value* of $name as the name of another variable.

It's a rare, advanced feature — genuinely useful in a handful of dynamic scenarios, but easy to overuse in ways that make code hard to trace.

Example: Variable Variables

php
<?php
// Declare `$name`, set to "color"
$name = "color";
$$name = "blue";
// Print `$color` to the output
echo $color;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the $ prefix, writing name = Ann;, which PHP treats as a constant assignment and fails to parse.
  2. Using a variable before assigning it, such as echo $total;, which raises an 'Undefined variable' warning and prints nothing.
  3. Expecting a global variable to be visible inside a function, when functions have their own scope and need global or a parameter to see it.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.