Data Types in PHP (with var_dump Output)
In this lesson: All 8 PHP data types with var_dump output — string, integer, float, boolean, array, object, NULL, resource. Hindi & English.
What is a data type
A data type is the kind of value a variable holds — is it text, a whole number, a decimal, a yes/no, or a list of items? PHP decides the type automatically from the value you assign, because it is loosely typed. But knowing the types helps you write correct code and understand what PHP is doing.
"18" (in quotes) is text, while 18 (no quotes) is a number. They look the same but behave differently.Scalar types (string, int, float, bool)
Scalar means a single value. These four are the ones you use every day:
<?php
$name = "Aman"; // string - text
$age = 18; // integer - whole number
$price = 99.5; // float - decimal number
$active = true; // boolean - true or false
?>
| Type | Example | Used for |
|---|---|---|
| string | "Aman" | Names, messages, any text |
| integer | 18 | Counts, ages, IDs |
| float | 99.5 | Prices, averages, measurements |
| boolean | true / false | Yes/no flags, conditions |
Array and NULL
An array holds many values in one variable. NULL means no value at all.
<?php
$marks = [85, 90, 78]; // array of numbers
$city = null; // no value yet
echo $marks[1]; // 90 (second item)
?>
Array items are numbered from 0, so $marks[1] is the second value. NULL is useful when a value does not exist yet, like a middle name that was left blank.
Checking a type (var_dump / gettype)
To see what PHP thinks a value is, use var_dump() (type + value) or gettype() (type name only).
<?php
$age = 18;
var_dump($age); // int(18)
echo gettype($age); // integer
?>
integer
var_dump is your best friend while debugging — it removes all guessing about whether a value is a number or text.
Type conversion (casting)
You can force a value into another type by writing the target type in brackets — this is called casting.
<?php
$text = "123";
$num = (int) $text; // now the number 123
echo $num + 1; // 124
?>
PHP also converts automatically in many cases (type juggling). For example "5" + 2 gives 7 because the string is turned into a number for the calculation.
Common mistakes
- Confusing "18" with 18. Text in quotes is a string, not a number; it can behave unexpectedly in maths.
- Thinking NULL equals 0. NULL means "no value"; 0 is an actual number.
- Using
==when you mean===.==ignores type (so"5" == 5is true), while===checks type too. - Forgetting arrays start at index 0. The first item is
[0], not[1].
Summary
- PHP's everyday types: string, integer, float, boolean, array and NULL.
- PHP picks the type automatically from the value (loosely typed).
- Use
var_dump()orgettype()to check a type. - Cast with
(int),(float), etc.; PHP also juggles types on its own. - Use
===for strict comparison that also checks the type.
Next, store many values together with arrays in PHP, or make decisions using if-else in PHP.
Frequently Asked Questions
How many data types does PHP have?
How do I check the type of a variable in PHP?
var_dump($x) to see both the type and the value, or gettype($x) to get just the type name as text. var_dump is the most useful while debugging because it shows exactly what PHP thinks a value is.What is the difference between integer and float?
What does NULL mean in PHP?
null to it, or if you use it before assigning anything. It is different from 0 or an empty string.Does PHP change types automatically?
=== for strict comparisons.