Data Types
Explore PHP's data types — scalar, compound, and special — and learn how to check the type of any value.
Introduction
Every value in PHP — a name, a price, a list of products, the result of a database query — belongs to a specific data type. Understanding these types is essential, because the type of a value determines what you can do with it and how PHP will behave when you compare, combine, or convert it.
This lesson gives you a complete overview of PHP's data types, groups them into scalar, compound, and special categories, and shows you the tools PHP provides to inspect a value's type at any point in your code.
- The full list of PHP's data types.
- How scalar, compound, and special types differ.
- How to check a value's type using gettype() and var_dump().
- A recap of how dynamic typing ties all of this together.
Overview of PHP Data Types
PHP has a handful of core data types that every value you work with will fall into.
string
Text data, such as "Hello" or 'PHP is fun'.
int
Whole numbers, positive or negative, such as 42 or -7.
float
Numbers with a decimal point, such as 3.14 or 0.18 (also called double).
bool
A truth value: either true or false.
array
An ordered collection of values, accessed by index or key.
object
An instance of a class, bundling data and behavior together.
null
A special type representing "no value at all."
<?php
$name = "Rohan"; // string
$age = 21; // int
$gpa = 8.75; // float
$isEnrolled = true; // bool
$subjects = ["Math", "Physics"]; // array
$nothingYet = null; // null
echo "$name is $age years old with a GPA of $gpa.";
?>Rohan is 21 years old with a GPA of 8.75.Scalar, Compound & Special Types
PHP's manual groups these data types into three broader categories, based on what kind of data they hold.
| Category | Types | Description |
|---|---|---|
| Scalar | string, int, float, bool | Hold a single, simple value. |
| Compound | array, object | Hold multiple values or combine data with behavior. |
| Special | null, resource | Represent the absence of a value, or an external reference like an open file handle. |
A resource is a special reference to an external entity, such as an open file or a database connection. You do not need to worry about it now — you will encounter it naturally once you start working with files and databases later in this course.
Checking a Value's Type
Since PHP is dynamically typed, it is often useful to check what type a variable currently holds. Two built-in tools make this easy: gettype() and var_dump().
gettype() returns the type of a value as a simple string.
<?php
$price = 499.99;
$count = 3;
$inStock = true;
echo gettype($price) . "\n";
echo gettype($count) . "\n";
echo gettype($inStock) . "\n";
?>double
integer
booleanvar_dump() goes further: it prints both the type and the value together, and works recursively for arrays, which makes it a favorite tool for debugging.
<?php
$data = ["name" => "Meera", "age" => 30, "active" => true];
var_dump($data);
?>array(3) {
["name"]=>
string(5) "Meera"
["age"]=>
int(30)
["active"]=>
bool(true)
}Use gettype() when you only need to know the type as a string, for example inside a conditional check. Use var_dump() while debugging, when you want to see both the type and the actual value at a glance.
Dynamic Typing Recap
As you saw in the previous lesson on variables, PHP does not require you to declare a type — a variable's type is simply whatever value is currently assigned to it, and it can change freely over time.
<?php
$value = "100"; // string
echo gettype($value) . "\n";
$value = 100; // now an int
echo gettype($value) . "\n";
$value = 100.0; // now a float
echo gettype($value) . "\n";
?>string
integer
doubleBecause PHP is dynamically typed, it also performs automatic type conversion in many situations — for example when comparing a string to a number. This behavior, called type juggling, is important enough that it gets its own dedicated lesson coming up soon: Type Casting.
Common Mistakes
- Confusing gettype()'s output "double" for floats with the word "float" — PHP internally calls this type double for historical reasons.
- Assuming a variable's type stays fixed just because it started as one type.
- Using var_dump() output directly in production error messages instead of during local debugging.
- Forgetting that null is its own distinct type, different from an empty string "" or the number 0.
Best Practices
- Use var_dump() liberally while debugging to inspect both type and value at once.
- Use gettype() when you need to branch your logic based on a value's type.
- Keep a variable's type consistent across your script wherever practical, even though PHP allows it to change.
- Remember that resource and object types will make more sense once you reach files, databases, and OOP later in the course.
Frequently Asked Questions
Why does gettype() return "double" instead of "float"?
For historical reasons, PHP internally refers to the float type as "double" (double-precision floating point), even though you declare and think of it as float.
Is null the same as an empty string?
No. null represents the complete absence of a value, while an empty string "" is still a string — just one with zero characters.
What is the difference between a scalar and a compound type?
A scalar type (string, int, float, bool) holds one single value. A compound type (array, object) can hold multiple values or combine data with behavior.
Should I use var_dump() or print_r() for debugging?
var_dump() shows both types and values and is best for precise debugging. print_r() shows a more human-readable structure but omits type information — many developers use both depending on the situation.
What is a resource type used for?
It represents a reference to an external resource, such as an open file handle or database connection, which you will encounter later in the course.
Key Takeaways
- PHP's core data types are string, int, float, bool, array, object, and null (plus resource).
- Types are grouped as scalar (single values), compound (arrays and objects), and special (null and resource).
- gettype() returns a value's type as a string; var_dump() shows both type and value, recursively for arrays.
- PHP's dynamic typing means a variable's type is simply whatever value it currently holds.
Summary
PHP organizes every value into a small set of data types: simple scalar types like string, int, float, and bool; compound types like array and object; and special types like null. Tools like gettype() and var_dump() let you inspect exactly what type and value a variable holds at any moment.
Next, you will learn about type casting — how PHP converts values between these types, both automatically and when you do it deliberately yourself.
- You know PHP's full set of core data types.
- You can classify types as scalar, compound, or special.
- You can inspect a value's type using gettype() and var_dump().
- You are ready to learn how PHP converts between types.