Data Types
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.
Learn Data Types in our free PHP course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.
Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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() .
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.
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.
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.
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.
Now you try. Fill each ___ so the printed type matches the comment, then run it.
🧩 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)
- An if ($price) never runs even though $price is "0" — the string "0" is falsy. Compare explicitly: if ($price !== "") or check with isset() .
- gettype() says "double" but you expected "float" — that's the legacy name. Use get_debug_type() for the modern, accurate float .
- Integer division gives a decimal — / always returns a float. Switch to intdiv($a, $b) when you want a whole number.
- is_numeric("42px") is unexpectedly false — that's correct: a string is only numeric if the whole thing parses as a number. Strip the unit first.
Pro Tips
- 💡 Default to get_debug_type() . It's the PHP 8 way and reports real class names for objects.
- 💡 Keep the falsy seven on a sticky note. false 0 0.0 "" "0" [] null — everything else is true.
- 💡 Reach for is_numeric() on user input before doing maths with it, since form data always arrives as strings.
📋 Quick Reference — Data Types
Type
Example
Notes
string
"hi"
Text, any length
int
42
Whole number
float
3.14
Decimal; gettype says "double"
bool
true
true / false
array
[1, 2]
List or map
object
new stdClass
Named properties
null
No value
resource
fopen(...)
External handle
Frequently Asked Questions
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.
🎉 Lesson Complete!
- ✅ PHP has eight types: four scalar, two compound, two special
- ✅ string , int , float , bool are the everyday scalars
- ✅ / always yields a float — use intdiv() for whole division
- ✅ Exactly seven values are falsy; everything else is truthy
- ✅ get_debug_type() is the modern way to inspect a value
- ✅ Next lesson: Strings & String Functions — slice, search, and format text
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"
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
- Previous: Variables
- Next: Strings & String Functions