PHP Variables

A variable contains a value that can be used over and over again within your PHP code. A variable can contain a numeric, alphabetical, or alphanumeric value.

PHP Variable Syntax

The correct syntax for creating a variable is:


$variablename = value;

Each variable must begin with a dollar sign ($).
Each variable name must begin with a letter or underscore (_).
Each variable name can only contain alphanumeric characters and underscores (_).
Variable names are case sensitive, so “VariableName” is different than “variablename”.

Variable Examples

The following are examples of PHP variables:


$firstname = "John";
$lastname = "Robinson";
$age = "26";

The variables can then be used later in the code:


echo $firstname;
echo $lastname;
echo $age;

This bit of code would display:

John
Robinson
26

Also see PHP strings.