W3docs

Introduction to PHP Variables

In PHP, a variable is a memory location used to store values. Variables in PHP are denoted by a dollar sign ($), followed by the name of the variable. The value

In PHP, a variable is a named container that stores a value in memory. Variables are denoted by a dollar sign ($) followed by the variable's name. The value a variable holds can be set, changed, and read at any point while a PHP script runs, which is what makes a script dynamic rather than a fixed block of output.

This chapter covers how to declare variables, the naming rules you must follow, the data types a variable can hold, how PHP decides a variable's type for you, and the everyday operations you'll perform on variables.

Declaring a Variable

A variable is created the moment you assign a value to it. PHP has no separate "declaration" keyword — assignment is declaration:

PHP define a variable

<?php
$variable_name = value;

Here $variable_name is the name and value is what you store in it. A complete, working example:

<?php
$greeting = "Hello, World!";
$year     = 2024;

echo $greeting;   // Hello, World!
echo "\n";
echo $year;       // 2024

Output:

Hello, World!
2024

You don't have to give a variable a value when you mention it, but reading a variable that was never assigned raises a warning and evaluates to NULL, so always assign before you use.

Naming Rules

A valid PHP variable name:

  • Starts with a letter or an underscore (_) — never a digit.
  • Contains only letters, numbers, and underscores after the first character.
  • Is case-sensitive: $name, $Name, and $NAME are three different variables.
<?php
$user_age = 30;   // valid
$_token   = "ab"; // valid (starts with underscore)
$2cool    = 1;    // INVALID — cannot start with a digit

By convention, PHP variables use snake_case ($first_name), though camelCase ($firstName) is also common. Pick one style and stay consistent.

Data Types a Variable Can Hold

PHP is dynamically typed: you never declare a type, and the same variable can hold different types over its lifetime. The type is inferred from the value you assign.

  • String — a sequence of characters: $name = "John Doe";
  • Integer — a whole number: $age = 25;
  • Float — a number with a decimal point: $average = 7.5;
  • Booleantrue or false: $is_active = true;
  • Array — an ordered collection of values: $fruits = ["apple", "banana", "orange"];
  • Object — an instance of a class: $person = new Person();
  • NULL — a variable with no value: $email = null;

You can inspect a variable's value and type at runtime with var_dump():

<?php
$age     = 25;
$average = 7.5;
$name    = "Sara";

var_dump($age);
var_dump($average);
var_dump($name);

Output:

int(25)
float(7.5)
string(4) "Sara"

For a deeper look at each type, see PHP Data Types.

Loose Typing and Type Juggling

Because a variable's type comes from its value, reassigning changes the type:

<?php
$x = 10;       // $x is an integer
$x = "ten";    // now $x is a string — perfectly legal
$x = 3.14;     // now $x is a float

PHP also converts types automatically in many expressions (called type juggling). For example, a numeric string is treated as a number in arithmetic:

<?php
$result = "5" + 3;
echo $result;   // 8 — the string "5" is converted to the integer 5

This convenience can surprise you, so when you need a value to be a specific type, cast it explicitly: (int) $value, (float) $value, or (string) $value.

Operations on Variables

Once you have variables, you combine and compare them.

Assignment — store a value:

<?php
$name = "John Doe";

Arithmetic — add, subtract, multiply, and so on:

<?php
$a = 8;
$b = 3;
$sum = $a + $b;
echo $sum;   // 11

Comparison — test the relationship between values; the result is a boolean:

<?php
$a = 5;
$b = 5;
$is_equal = ($a == $b);
var_dump($is_equal);   // bool(true)

Concatenation — join strings with the dot (.) operator:

<?php
$first_name = "John";
$last_name  = "Doe";
$full_name  = $first_name . " " . $last_name;
echo $full_name;   // John Doe

For the full list of operators, see PHP Operators.

Variable Scope

Where you declare a variable determines where it can be read. A variable created in the main body of a script is not automatically available inside a function, and vice versa:

<?php
$message = "outside";

function show() {
    echo $message;   // Notice: undefined variable — scope does not reach in
}

show();

To use an outer variable inside a function, pass it as an argument or import it with the global keyword. Scope is a common source of bugs for beginners, so it's worth reading the dedicated chapter on PHP Variable Scope.

Constants vs. Variables

When a value must never change, use a constant instead of a variable. Constants have no $ prefix and are defined with define() or the const keyword. See PHP Constants for the details.

Conclusion

PHP variables are the building blocks of every script: they store and manipulate the data your program works with. The key takeaways are that variables start with $, names are case-sensitive and cannot begin with a digit, and PHP infers the type from the value so a single variable can hold strings, numbers, arrays, objects, or NULL over its lifetime. From here, continue with PHP Data Types and PHP Operators to put variables to work.

Practice

Practice
Which of the following statements are true about PHP variables?
Which of the following statements are true about PHP variables?
Was this page helpful?