PHP Tutorial · PHP Basics · Lesson 7 of 38

PHP Data Types

Strings, integers, floats, booleans, arrays, objects and null.

PHP's types

Type Example
string "Hello"
int 42, -7, 0x1A
float 3.14, 1.5e3
bool true, false
array [1, 2, 3], ["a" => 1]
object new DateTime()
null null — no value

var_dump() shows a value's type and value; gettype() and get_debug_type() return the type name:

Example 1Try it yourself
<?php
$values = ["text", 42, 3.14, true, [1, 2], null];
foreach ($values as $v) {
    echo get_debug_type($v), "\n";
}
var_dump(10 / 4);

Output

string
int
float
bool
array
null
float(2.5)

Type checks

Functions like is_int(), is_string(), is_array() and is_null() test types:

Example 2Try it yourself
<?php
var_dump(is_int(5), is_int("5"), is_numeric("5"), is_array([]), is_null(null));

Output

bool(true)
bool(false)
bool(true)
bool(true)
bool(true)

Strict types

PHP converts types automatically in many places. Adding declare(strict_types=1); at the top of a file makes typed function parameters reject the wrong type instead of converting silently — recommended for new code.

Test yourself

What does var_dump(10 / 4) print?

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

Answer: float(2.5)