A variable is a named container for a value. You store something in it, then use the name wherever you need that value.
Creating variables
Variables in PHP start with a dollar sign. Assign a value with =:
<?php
$name = 'Asha';
$age = 29;
$isMember = true;
echo $name;
You do not declare a type in advance. PHP works out the type from the value.
Assigning again replaces the value:
<?php
$score = 10;
$score = 15;
echo $score; // 15
Naming rules
- Start with a letter or underscore:
$total,$_count. - Then use letters, digits and underscores:
$item2,$first_name. - No spaces, hyphens or leading digits:
$first-nameand$2ndare invalid. - Names are case-sensitive:
$userand$Userare different.
Pick names that describe the content. $unpaidInvoiceTotal is longer than $x, but you will never wonder what it holds. PHP projects usually use camelCase ($firstName); WordPress uses snake_case ($first_name). Follow the project you are in.
The main data types
| Type | Example | Used for |
|---|---|---|
int | 42, -7 | Whole numbers |
float | 3.14, 0.5 | Numbers with decimals |
string | 'hello' | Text |
bool | true, false | Yes or no values |
array | ['a', 'b'] | Lists and maps of values |
null | null | "No value" |
object | new DateTime() | Instances of classes |
Check a value and its type with var_dump(), or get just the type name with get_debug_type():
<?php
$price = 9.99;
var_dump($price); // float(9.99)
echo get_debug_type($price); // float
echo get_debug_type('9.99'); // string
echo get_debug_type(null); // null
null
null means a variable has no value. It is common for "not set yet" or "not found":
<?php
$middleName = null;
if ($middleName === null) {
echo 'No middle name given';
}
Type juggling
PHP converts types automatically when an operation needs it. This is called type juggling:
<?php
$result = '5' + 3;
var_dump($result); // int(8)
The string '5' became the number 5. Convenient, but it can hide bugs:
<?php
var_dump('10 apples' + 5);
Warning: A non-numeric value encountered...
int(15)
PHP 8 warns about strings like this instead of silently guessing, which is a big improvement over older versions.
Strict types
At the very top of a file you can turn on strict mode for function arguments. With it, PHP refuses to convert types when calling functions that declare parameter types:
<?php
declare(strict_types=1);
function double(int $n): int
{
return $n * 2;
}
echo double(4); // 8
echo double('4'); // TypeError
Many teams put declare(strict_types=1); at the top of every file. It turns silent conversions into clear errors that are easy to fix.
Converting types yourself
Cast a value when you need a specific type, for example when reading form input, which always arrives as strings:
<?php
$quantity = (int) '3'; // int(3)
$price = (float) '4.50'; // float(4.5)
$label = (string) 125; // string(3) "125"
$flag = (bool) 0; // bool(false)
Constants
A constant holds a value that never changes. By convention constant names are UPPERCASE:
<?php
const VAT_RATE = 0.2;
$net = 100;
echo $net * (1 + VAT_RATE); // 120
Constants have no dollar sign and cannot be reassigned. PHP also defines useful built-in constants, such as PHP_VERSION, PHP_EOL (the newline for the current system) and PHP_INT_MAX.
Summary
- Variables start with
$and take their type from their value. - The main types are int, float, string, bool, array, null and object.
var_dump()andget_debug_type()show what a value really is.- PHP converts types automatically;
declare(strict_types=1);makes function calls strict. - Use constants (
const NAME = value;) for fixed values.