Data Types

Reviewed & published by Brayan K

Every value in PHP has a type. In this lesson you'll meet all eight, learn the four you'll use every single day, discover exactly which values count as falsy, and pick the right tool — get_debug_type, var_dump, is_* — to inspect any value.

Part of the free PHP course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ The Eight Types at a Glance

PHP sorts every value into one of eight types, grouped into three families. The four scalar types each hold a single value. The two compound types — array and object — hold collections. And two special types stand apart: null (no value at all) and resource (a live handle to a file, socket, or database). The cleanest way to ask PHP what something is, since PHP 8, is get_debug_type().

<?php
// PHP groups every value into one of eight types.
// Four SCALAR types hold a single value:
$word    = "hello";   // string  — text
$count   = 42;        // int     — whole number
$ratio   = 3.14;      // float   — decimal (a.k.a. "double")
$active  = true;      // bool    — true or false

// Two COMPOUND types hold many values:
$list    = [1, 2, 3];                 // array  — ordered collection
$obj     = new stdClass();            // object — a bundle of properties

// Two SPECIAL types stand apart:
$nothing = null;                      // null   — the absence of a value
$handle  = fopen("php://memory","r"); // resource — a live external handle

// get_debug_type() (PHP 8) is the cleanest way to ask "what is this?".
echo get_debug_type($word)   . "\n";
echo get_debug_type($count)  . "\n";
echo get_debug_type($ratio)  . "\n";
echo get_debug_type($active) . "\n";
echo get_debug_type($list)   . "\n";
echo get_debug_type($obj)    . "\n";
echo get_debug_type($nothing). "\n";
fclose($handle);
?>

Notice that get_debug_type() returns the real class name (stdClass) for objects — far more useful than the bare word object you'd get from gettype().

2️⃣ The Four Scalar Types

Scalars are the atoms of PHP: string (text), int (whole numbers), float (decimals), and bool (true/false). The dividing line between int and float is the decimal point — and there's one classic surprise hiding in division.

<?php
// The four scalar types, dumped so you see type AND value.
$name   = "Ada Lovelace";
$year   = 1815;
$height = 1.68;
$genius = true;

var_dump($name);   // string(12) "Ada Lovelace"
var_dump($year);   // int(1815)
var_dump($height); // float(1.68)
var_dump($genius); // bool(true)

// Ints are whole numbers; the moment you add a decimal point you get a float.
var_dump(10);      // int(10)
var_dump(10.0);    // float(10)
var_dump(10 / 2);  // int(5) — / gives an int when it divides exactly
var_dump(10 / 3);  // float(3.3333333333333335) — otherwise a float
?>

The headline: / always returns a float, even when the result is a whole number. 10 / 2 is float(5). If you need a true integer result, use intdiv(10, 2) instead.

3️⃣ Truthy & Falsy

Whenever a value sits in a boolean context — inside an if, a while, or a ternary — PHP converts it to true or false. There are exactly seven falsy values; everything else is truthy. Learn this short list and a whole category of bugs disappears.

<?php
// PHP treats certain values as "falsy" in a boolean context
// (inside if, while, ?:, etc.). EVERYTHING ELSE is truthy.
// The complete falsy list — memorise it:
//   false   0   0.0   ""   "0"   []   null

$falsy = [false, 0, 0.0, "", "0", [], null];
foreach ($falsy as $v) {
    echo var_export($v, true) . " => " . ((bool) $v ? "true" : "false") . "\n";
}

// Two famous gotchas that ARE truthy:
var_dump((bool) "0.0");  // true  — it's a non-empty, non-"0" string
var_dump((bool) "false");// true  — any non-empty string except "0" is true
?>

The two traps catch everyone once: the string "0" is falsy, but "0.0" and "false" are truthy — they're non-empty strings that aren't exactly "0".

4️⃣ Compound Types: Arrays & Objects

When you need to hold many values together, you reach for a compound type. An array can be an ordered list or a key-value map — both report as array. An object bundles named properties; the built-in stdClass is a generic, empty object you can stick properties onto.

<?php
// ARRAYS are the workhorse compound type. An array can be a simple
// list (numeric keys) or a map (string keys) — both are "array".
$colors = ["red", "green", "blue"];          // list
$ages   = ["ada" => 36, "alan" => 41];       // associative map

var_dump(get_debug_type($colors));           // string(5) "array"
echo count($colors) . " colors\n";          // 3 colors
echo $ages["alan"] . "\n";                  // 41

// OBJECTS bundle named properties. stdClass is the generic, empty class.
$user = new stdClass();
$user->name = "Grace";
$user->role = "admin";
echo "{$user->name} is {$user->role}\n";   // Grace is admin
echo get_debug_type($user) . "\n";          // stdClass
?>

5️⃣ Inspecting Types

You'll constantly need to ask "what type is this?" Use get_debug_type() for an accurate name, the is_* family (is_int, is_string, …) for yes/no checks, and is_numeric() to detect numbers stored inside strings.

<?php
// Four ways to ask "what type is this?" — pick the right tool.
$value = 4.5;

echo gettype($value)        . "\n"; // double   (legacy name for float!)
echo get_debug_type($value) . "\n"; // float    (PHP 8 — accurate, modern)
var_dump(is_float($value));          // bool(true)
var_dump(is_int($value));            // bool(false)

// is_numeric() spots numbers hiding inside strings.
var_dump(is_numeric("42"));          // bool(true)
var_dump(is_numeric("42px"));        // bool(false)
?>

Now you try. Fill each ___ so the printed type matches the comment, then run it.

<?php
// 🎯 YOUR TURN — fill each ___ so the printed type matches the comment.

$temperature = ___;   // 👉 a float like 21.5
$cityName    = ___;   // 👉 a string in "double quotes"
$isRaining   = ___;   // 👉 a bool: true or false

echo get_debug_type($temperature) . "\n";  // should print: float
echo get_debug_type($cityName)    . "\n";  // should print: string
echo get_debug_type($isRaining)   . "\n";  // should print: bool

// ✅ Expected output:
//    float
//    string
//    bool
?>

🧩 Reorder Challenge

These lines are scrambled. They should build an array, count it, and print whether it's empty. Put them in the order that runs correctly.

You must create the array before you can count it, and you must compute $n and $isEmpty before echoing them. Reading a variable before it's assigned would trigger an "Undefined variable" warning, so every value has to be produced before it's used. Output: Count: 3 then has items.

🧠 Quick Recall

Predict the output before you reveal each answer.

Prints float(5). The / operator always returns a float, even when the result is whole. Use intdiv(10, 2) for int(5).

Prints bool(true). Only the string "0" is falsy — "0.0" is a different, non-empty string, so it's truthy.

Prints array. Both lists and associative maps report as array — there's only one array type in PHP.

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — Data Types

TypeExampleNotes
string"hi"Text, any length
int42Whole number
float3.14Decimal; gettype says "double"
booltruetrue / false
array[1, 2]List or map
objectnew stdClassNamed properties
nullnullNo value
resourcefopen(...)External handle

Mini-Challenge: Type Report

Write it yourself, run it on onecompiler.com/php or your own machine, then check against the expected output in the comments.

<?php
// 🎯 MINI-CHALLENGE: a "type report" for a mixed bag of values.
// No code filled in — work from the steps, then run and compare.
//
// 1. Make an array $mixed holding one of each: a string, an int,
//    a float, a bool, and null.  (e.g. ["hi", 7, 1.5, true, null])
// 2. foreach over $mixed.
// 3. For each item echo a line:  <value> is a <type>
//    — use var_export($item, true) for the value
//    — use get_debug_type($item) for the type
//
// ✅ Expected output:
//    'hi' is a string
//    7 is a int
//    1.5 is a float
//    true is a bool
//    NULL is a null

// your code here
?>

🎉 Lesson Complete!

Practice quiz

How many data types does PHP have in total?

  • Four
  • Six
  • Eight
  • Ten

Answer: Eight. PHP has eight types: four scalar, two compound (array, object), and two special (null, resource).

Which group do string, int, float, and bool belong to?

  • Compound types
  • Scalar types
  • Special types
  • Reference types

Answer: Scalar types. string, int, float, and bool are the four scalar types — each holds a single value.

Which PHP 8 function gives the cleanest, most accurate type name?

  • gettype()
  • typeof()
  • get_debug_type()
  • var_dump()

Answer: get_debug_type(). get_debug_type() (PHP 8) returns modern names like 'int' and 'float', and real class names for objects.

Which of these values is NOT falsy in PHP?

  • 0
  • "" (empty string)
  • "0.0"
  • [] (empty array)

Answer: "0.0". The string "0.0" is a non-empty string that isn't exactly "0", so it is truthy.

Exactly how many values does PHP treat as falsy?

  • Five
  • Six
  • Seven
  • Eight

Answer: Seven. There are exactly seven falsy values: false, 0, 0.0, "", "0", [], and null.

What does gettype() report for a float, by historical quirk?

  • float
  • double
  • real
  • decimal

Answer: double. gettype() returns the legacy name 'double' for a float.

What does var_dump(is_numeric("42px")) output?

  • bool(true)
  • bool(false)
  • int(42)
  • NULL

Answer: bool(false). is_numeric is only true when the WHOLE string parses as a number; "42px" does not.

What does echo get_debug_type([1, 2]); print?

  • list
  • array
  • object
  • int

Answer: array. Both lists and associative maps report as 'array' — there is only one array type.

Which two types are PHP's COMPOUND types?

  • int and float
  • array and object
  • null and resource
  • string and bool

Answer: array and object. The compound types — array and object — group many values together.

What does var_dump((bool) "0") output?

  • bool(true)
  • bool(false)
  • string(1)
  • int(0)

Answer: bool(false). The string "0" is one of the seven falsy values, so casting it to bool gives false.

Continue this course

Frequently asked questions

What are the eight data types in PHP?

PHP has four scalar types (string, int, float, bool), two compound types (array, object), and two special types (null and resource). Scalar types hold a single value; compound types group many values; null represents the absence of a value; and a resource is a handle to something external like an open file or database connection. In everyday code you'll spend almost all your time with string, int, float, bool, array, and null.

What is the difference between int and float in PHP?

An int is a whole number with no decimal point (42, -7, 0). A float is a number with a fractional part (3.14, 1.0, -0.5) — PHP stores it as a double-precision number, which is why gettype() confusingly reports 'double'. A key surprise: dividing two ints with / always produces a float, so 10 / 2 is float(5), not int(5). Use intdiv(10, 2) when you specifically want integer division.

Which values are considered falsy in PHP?

Exactly seven values are falsy: false, the integer 0, the float 0.0, the empty string "", the string "0", the empty array [], and null. Everything else is truthy — including the string "0.0", the string "false", and the array [0]. Knowing this list by heart prevents a whole class of bugs in if-statements and short-circuit logic.

Should I use gettype() or get_debug_type()?

Prefer get_debug_type(), added in PHP 8. It returns accurate, modern names like 'int', 'float', and 'bool', and for objects it returns the actual class name. gettype() still exists but returns legacy names — 'integer' instead of 'int', 'double' instead of 'float', and just 'object' for every object — which makes it awkward for precise checks.