Variables, data types and arrays explained clearly
Three terms that appear in every PHP project. This text explains them with runnable examples and shows the places where even experienced developers still slip up.
What you will learn
Variables: a name for a value
A variable is a name under which a value is stored. In PHP, every variable name begins with a dollar sign. You do not have to specify a type — PHP infers it from the value.
$anzahl = 3; // int
$preis = 19.99; // float
$name = 'Auftrag'; // string
$offen = true; // bool
$bearbeiter = null; // null
echo gettype($anzahl); // integer
echo gettype($preis); // doubleThe fact that you do not have to specify the type does not mean you should not. In function signatures and properties, type declarations have been common for years and catch errors before they reach data.
The data types you meet in everyday work
The distinction between number and text is not a formality. Storing a postcode as a number loses the leading zero, and an article number as a number loses it just the same. Identifiers for which leading zeros or the format matter (postcode, article number, telephone number) belong in a string.
Arrays: one kind for two tasks
Other languages separate list and mapping. PHP has only one array, which can do both: it is always an ordered map from keys to values. Without keys of your own, PHP assigns consecutive numbers starting at 0.
// List — keys 0, 1, 2 arise automatically
$farben = ['rot', 'gruen', 'blau'];
echo $farben[1]; // gruen
// Map — custom keys
$auftrag = [
'nummer' => 10244,
'kunde' => 'Beispiel GmbH',
'offen' => true,
];
echo $auftrag['kunde']; // Beispiel GmbH
// Append
$farben[] = 'gelb';
echo count($farben); // 4Nested arrays are the normal case as soon as real data is involved: a list of orders is an array whose values are arrays again. Beyond a certain depth, a class is worth using instead — its name says what it contains, and a typo in a key is noticed immediately.
An example from practice
Three comparison pitfalls
// 1. Loose equality compares after conversion
var_dump(0 == 'abc'); // false (since PHP 8)
var_dump('1' == '01'); // true — both become numbers
var_dump('1' === '01'); // false — type and value
// 2. Floats are not exact
var_dump(0.1 + 0.2 == 0.3); // false
// 3. null, 0 and '' are all "empty"
var_dump(empty('0')); // true — surprises manyThe lesson from all three: compare scalars (numbers, strings, booleans) with three equals signs; for objects, === only checks whether it is the same object, so here you compare the values specifically or use a method such as equals()., check for null with `$wert === null` instead of with `empty()`, and do not calculate monetary amounts with floats, but in whole cents or with a fixed-point library (e.g. BCMath or a money class) if fractions of a cent also occur..
Common misconceptions
Further sources
- PHP manual: Types the official overview of all data types
- PHP manual: Arrays structure, keys and behaviour
- PHP manual: Comparison operators the table behind the pitfalls above
About the author
Fabian Lücker is managing partner of PHP-Schmiede KG in Rheine and has worked in software development since 2007. He is responsible for development and concept design and writes here about decisions that come up in real projects.