TExcelTAD
Home › PHP › PHP Data Types

PHP Data Types

Every value in PHP has a type. The type decides what you can do with the value: you can multiply integers, join strings, and loop over arrays. PHP picks the type automatically from the value you assign.

Overview

Type Example Description
string "hello", 'hi' Text (a sequence of bytes)
int 42, -7, 0x1F Whole numbers
float 3.14, 1.5e3 Numbers with a decimal point (also called double)
bool true, false Logical yes/no
array [1, 2, 3], ["a" => 1] Ordered map of keys to values
object new DateTime() An instance of a class
null null "No value"
resource fopen(...) result A handle to something external (file, stream)

The first four are called scalar types because they hold a single value.

Checking a type

var_dump() shows type and value together. gettype() returns the type name as a string, and get_debug_type() (PHP 8) returns the modern, shorter name:

php · try it yourself
<?php
$values = ["Paris", 12, 9.81, true, null, [1, 2]];

foreach ($values as $v) {
    echo str_pad(get_debug_type($v), 7), "| ", gettype($v), "\n";
}

var_dump(12, 9.81, "Paris", false);
string | string
int    | integer
float  | double
bool   | boolean
null   | NULL
array  | array
int(12)
float(9.81)
string(5) "Paris"
bool(false)

gettype() uses older names (integer, double) for historical reasons. In type declarations you always write int, float, bool and string.

Strings

A string is text in single or double quotes. Double quotes understand variables and escape sequences like \n; single quotes are taken literally:

php
$a = "Tab:\tend";   // contains a real tab
$b = 'Tab:\tend';   // contains a backslash and a t

Integers and floats

Integers are whole numbers, written in decimal, hexadecimal (0x), octal (0o) or binary (0b). Underscores may be used for readability. Floats have a decimal point or an exponent:

php · try it yourself
<?php
$population = 2_100_000;
$hex = 0xFF;
$binary = 0b1010;
$pi = 3.14159;
$small = 2.5e-3;

var_dump($population, $hex, $binary, $pi, $small);
var_dump(10 / 4, 10 / 5, intdiv(10, 4));
int(2100000)
int(255)
int(10)
float(3.14159)
float(0.0025)
float(2.5)
int(2)
int(2)

Notice that / returns an int when the division is exact and a float otherwise. intdiv() always gives an integer.

Booleans

A boolean is true or false, often the result of a comparison. When PHP needs a boolean (for example in an if), it converts other values. These are falsy: false, 0, 0.0, "", "0", [] and null. Everything else is truthy.

php · try it yourself
<?php
$tests = [0, 1, "", "0", "false", [], [0], null, -3];

foreach ($tests as $t) {
    echo str_pad(json_encode($t), 8), "=> ", $t ? "truthy" : "falsy", "\n";
}
0       => falsy
1       => truthy
""      => falsy
"0"     => falsy
"false" => truthy
[]      => falsy
[0]     => truthy
null    => falsy
-3      => truthy

Note: The string "false" is truthy — it is a non-empty string. Only the exact string "0" and the empty string are falsy.

Arrays

An array stores many values under keys. Keys can be integers (an indexed list) or strings (an associative map). Arrays get several lessons of their own later:

php
$colors = ["red", "green", "blue"];
$person = ["name" => "Lea", "age" => 31];

Objects

An object is an instance of a class — a blueprint that bundles data and behaviour:

php · try it yourself
<?php
class Car {
    public function __construct(public string $brand, public int $year) {}
}

$car = new Car("Volvo", 2021);
echo $car->brand . " from " . $car->year . "\n";
var_dump($car instanceof Car);
echo get_debug_type($car), "\n";
Volvo from 2021
bool(true)
Car

NULL

null is the only value of the type null. A variable is null when it has been assigned null or has been unset(). It is how PHP says "nothing here".

Type juggling

Because PHP is dynamically typed, it converts values automatically when an operation needs a different type: "5" + 3 gives 8. This is convenient but can hide bugs. You can make function parameters stricter with type declarations and by placing declare(strict_types=1); at the top of a file — more on that in the Casting and Functions lessons.

Test yourself

What does var_dump(10 / 4); print?

  1. int(2)
  2. float(2.5)
  3. int(3)
  4. string(3) "2.5"

Which value is **truthy**?

  1. "0"
  2. []
  3. "false"
  4. 0.0

Which of these is **not** a scalar type?

  1. int
  2. bool
  3. string
  4. array