Variables are “containers” for storing data, enabling you to manage and manipulate information within your scripts.
Creating (Declaring) PHP Variables
In PHP, a variable starts with the $
sign followed by the name of the variable.
Example
<?php
$x = 5;
$y = "Honda";
?>
In the example above, the variable $x
holds the value 5
, and $y
holds the value "Honda"
.
Note: When assigning a string value to a variable, enclose the value in quotes.
Note: PHP does not require a separate command for declaring variables. A variable is created the moment you assign a value to it.
Think of variables as containers for storing different types of data.
Rules for PHP Variables
- A variable starts with the
$
sign, followed by the name of the variable. - A variable name must begin with a letter or an underscore (
_
). - A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and
_
). - Variable names are case-sensitive (
$car
and$CAR
are two different variables).
Outputting Variables
The PHP echo
statement is commonly used to output data to the screen.
Example: Output Text and a Variable
<?php
$vehicle = "motorcycle";
echo "I ride a $vehicle!";
?>
Example: Concatenating Strings
The following example produces the same output by concatenating strings:
<?php
$vehicle = "motorcycle";
echo "I ride a " . $vehicle . "!";
?>
Example: Output the Sum of Two Variables
<?php
$x = 5;
$y = 4;
echo $x + $y; // Outputs 9
?>
Note: You will learn more about the
echo
statement and how to output data in the PHP Echo/Print chapter.
PHP is a Loosely Typed Language
In the examples above, you did not need to specify the data type of the variables. PHP automatically determines the data type based on the variable’s value. This flexibility allows you to, for example, add a string to an integer without causing an error.
In PHP 7, type declarations were introduced. This feature lets you specify the expected data type when declaring a function. Enabling strict requirements will trigger a “Fatal Error” on a type mismatch.
You will learn more about strict and non-strict requirements, and data type declarations in the PHP Functions chapter.
Variable Types
PHP supports various data types, and the data type depends on the value assigned to the variable.
Example
<?php
$x = 5; // $x is an integer
$y = "Yamaha"; // $y is a string
echo $x;
echo $y;
?>
PHP supports the following data types:
- String
- Integer
- Float (floating point numbers – also called double)
- Boolean
- Array
- Object
- NULL
- Resource
Getting the Variable Type
To determine the data type of a variable, use the var_dump()
function.
Example
The var_dump()
function returns both the data type and the value:
<?php
$x = 5;
var_dump($x);
?>
Example: var_dump()
for Other Data Types
<?php
var_dump(5);
var_dump("Harley-Davidson");
var_dump(3.14);
var_dump(true);
var_dump(array("car", "motorcycle", "bicycle"));
var_dump(NULL);
?>
Assigning Strings to Variables
To assign a string to a variable, use the variable name followed by an equal sign and the string.
Example
<?php
$brand = "Toyota";
echo $brand;
?>
String variables can be declared using either double or single quotes, but there are differences between the two. Learn more about these differences in the PHP Strings chapter.
Assigning Multiple Values
You can assign the same value to multiple variables in a single line.
Example
All three variables get the value “Vehicle”:
<?php
$x = $y = $z = "Vehicle";
?>