Skip to content

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.

  • Who it is forBeginners, technically interested readers
  • LevelBasics
  • Reading timeabout 7 minutes
  • Last updated16 October 2026

What you will learn

  • What a variable is and how PHP deals with types
  • Which data types exist and when to use which
  • How arrays are structured — and why there is only one kind
  • Three comparison pitfalls that cause real bugs

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.

PHPtested with PHP 8.3
$anzahl = 3;            // int
$preis = 19.99;         // float
$name = 'Auftrag';      // string
$offen = true;          // bool
$bearbeiter = null;     // null

echo gettype($anzahl);  // integer
echo gettype($preis);   // double
gettype() returns the type at runtime. "double" is the historical name for float there.

The 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

  • int — whole numbers: counts, identifiers, years.
  • float — decimal numbers. Use with caution for monetary amounts, more on that below.
  • string — text. A postcode or an article number is also text, not a number.
  • bool — true or false.
  • array — an ordered collection of values.
  • null — "no value". Not the same as 0 or an empty string.
  • object — an instance of a class.

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.

PHPtested with PHP 8.3
// 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);        // 4
Both forms are the same language construct. That is why they can also be mixed — which you should still rarely do.

Nested 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

PHPtested with PHP 8.3
// 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 many
var_dump() shows type and value. The first case behaved differently before PHP 8: 0 == "abc" was true there.

The 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

PHP has types, you just do not have to write them down. With `declare(strict_types=1)` at the top of the file, PHP even checks type declarations strictly instead of converting silently.

It can be a list. Technically it is always a map from keys to values — which is why gaps remain in the numbering after an element is removed.

They are not: 0.1 + 0.2 does not give exactly 0.3. In accounting, such deviations add up.

Further sources

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.