Variables are a key area to learn in PHP. They can be used to store data, perform calculations and process form and database data.
Unlike some other languages, there is no requirement to state a variable’s data type in PHP.
Declaring Variables
All PHP variables are preceded by a dollar sign ($). They then have a name.
<?php
$name = "Michael";
$age = 32;
$price = 49.90;
echo $name;
?>
This will display:
Michael
Variables must start with a letter or an underscore. Numbers and spaces cannot be used as the initial character.
As well as being case sensitive, ($name) and ($Name) are also recognised as separate variables.
PHP Variable Data Types
PHP automatically recognises what data type a variable is based on the value that has been stored in it.
| Data type | Example | Purpose |
|---|---|---|
| String | $name = "John"; | Text |
| Integer | $quantity = 5; | Whole numbers |
| Float | $price = 19.95; | Decimal numbers |
| Boolean | $active = true; | True or false values |
| Array | $colors = ["red", "blue"]; | Collections of values |
| Object | $user = new User(); | Class instances |
| NULL | $result = null; | Absence of a value |
| Resource | $file = fopen("data.txt", "r"); | References to external resources |
This is especially helpful when analysing why a PHP script may be generating incorrect data.
Carrying Out Calculations Using Variables
An e-commerce site may use three variables to calculate how much a customer has to pay for their order.
<?php
$price = 25;
$quantity = 3;
$shipping = 5;
$total = ($price $quantity) + $shipping;
echo "Total: $" . $total;
?>
Output:
Total: $80
Both multiplication and addition are used in this calculation. The dot (.) is used to merge text and numeric data.
For financial PHP scripts, it can be good practice to store money values as integers representing the number of cents as this avoids issues relating to decimal places that can occur with floats.
Variables in Strings
PHP treats single and double quotes in slightly different ways.
<?php
$name = "Anna";
echo "Hello $name";
echo 'Hello $name';
?>
Whilst the first line outputs ‘Hello Anna’, the second outputs ‘Hello $name’.
Double quotes allow for variables to be included in strings. Usually, single quotes do not.
Brackets can additionally be used to unambiguously denote where variables start and end in more complex scenarios:
echo "Welcome, {$name}!";
Variable Scope
Variables defined outside of a function are not automatically recognised inside the function.
<?php
$price = 100;
function calculateDiscount($amount) {
return $amount * 0.20;
}
echo calculateDiscount($price);
?>
Output:
20
Usually, it is preferable to pass variables to a function rather than refer to global variables as this can make PHP scripts simpler to manage.
PHP can also define static variables which ‘remember’ their values between function calls within the same PHP script.
Accessing Variables from HTML Forms
PHP includes ‘superglobal’ variables which enable data from web forms to be accessed.
First, include an HTML form in your PHP script:
Submit
Then add the following PHP code to the same document:
<?php
$username = $_POST['username'] ?? '';
echo htmlspecialchars(
$username,
ENT_QUOTES,
'UTF-8'
);
?>
The ?? operator ensures that an empty string is used if the relevant form data cannot be found.
The htmlspecialchars() function converts characters so they are displayed correctly in HTML. This can also help to guard against cross-site scripting attacks where user-entered data is shown on an HTML page.
Whilst it may be tempting to, always verify form data as it cannot always be guaranteed to be reliable.
Common PHP Variable Issues
Novices often struggle with undeclared, incorrectly named and incorrectly converted variables.
The isset() function can be used to establish if a variable has been defined and is not NULL. Where both the variable’s value and data type are important, a strict comparison (===) should be made.
For instance:
var_dump(10 == "10"); // true
var_dump(10 === "10"); // false
As the data types differ (one is an integer and the other a string), the second comparison yields false.
Further examples can be viewed on the official PHP variables documentation.









