PHP Data Types Explained: A Beginner's Guide with Examples
Last updated 4 months ago | 279 views 75 5

Introduction: Why PHP Data Types Matter
In PHP, data types define the kind of data a variable can store, such as text, numbers, or more complex structures like arrays and objects. While PHP is a loosely typed language, meaning you don’t need to declare data types explicitly, understanding how they work is crucial.
Why? Because:
-
It helps you write better, bug-free code
-
It ensures correct data manipulation
-
It makes you more confident with type conversions, comparisons, and validations
This article walks you through PHP's eight data types, with real-world examples, a comparison table, and tips to avoid common mistakes.
PHP Data Types Overview
PHP has 8 primary data types, grouped into three categories:
Category | Data Types |
---|---|
Scalar | string , integer , float (double) , boolean |
Compound | array , object |
Special | NULL , resource |
✍️ Scalar Data Types
✅ 1. String
A sequence of characters enclosed in single or double quotes.
<?php
$name = "John Doe"; // String variable
echo "Hello, " . $name;
?>
✅ 2. Integer
Whole numbers, positive or negative, without decimals.
$age = 30; // Integer
✅ 3. Float (Double)
Numbers with decimal points.
$price = 49.99; // Float
✅ 4. Boolean
Represents true
or false
.
$isLoggedIn = true;
Compound Data Types
✅ 5. Array
A variable that holds multiple values.
$colors = array("Red", "Green", "Blue");
// Access with index
echo $colors[0]; // Red
✅ 6. Object
An instance of a user-defined class.
class Car {
public $brand = "Toyota";
}
$myCar = new Car();
echo $myCar->brand; // Toyota
Special Data Types
✅ 7. NULL
A variable with no value.
$deletedUser = NULL;
✅ 8. Resource
A reference to an external resource (like a file or database connection).
$handle = fopen("data.txt", "r"); // File resource
Comparison Table: PHP Data Types
Data Type | Example Value | Description |
---|---|---|
String | "Hello" |
Text, characters |
Integer | 100 |
Whole number |
Float | 99.99 |
Decimal number |
Boolean | true , false |
Logic-based true/false |
Array | array(1, 2, 3) |
List of values |
Object | new Car() |
Instance of a class |
NULL | NULL |
Empty or no value |
Resource | fopen("file.txt", "r") |
External reference like file or DB |
Complete Example Using All PHP Data Types
<?php
// Scalar Types
$name = "Alice"; // String
$age = 25; // Integer
$height = 5.7; // Float
$isStudent = true; // Boolean
// Compound Types
$subjects = array("Math", "Science", "PHP"); // Array
class Student {
public $university = "StudyZone University";
}
$student = new Student(); // Object
// Special Types
$project = NULL; // NULL
$file = fopen("sample.txt", "r"); // Resource
// Output
echo "Name: $name<br>";
echo "Age: $age<br>";
echo "Height: $height ft<br>";
echo "Student: " . ($isStudent ? "Yes" : "No") . "<br>";
echo "Subjects: " . implode(", ", $subjects) . "<br>";
echo "University: " . $student->university . "<br>";
?>
⚠️ Tips & Common Pitfalls
✅ Best Practices
-
Use
gettype($variable)
orvar_dump($variable)
to inspect types. -
Prefer explicit casting when needed:
(int)$price
-
Use strict comparison (
===
) to avoid type juggling bugs.
❌ Common Mistakes
-
Assuming
"5" + 5
results in string—it actually returns10
(auto-casting) -
Forgetting
NULL
values can affect conditions (if ($var)
is false if null) -
Confusing
==
with===
when comparing values
Type Casting in PHP
You can convert between types manually:
$number = "10"; // String
$converted = (int)$number; // Integer
Conclusion: Master the Foundation
Understanding PHP data types is foundational for everything else you'll build. Whether you’re validating forms, creating APIs, or working with databases—knowing what type your data is, and how PHP treats it, is vital.