PHP Data Types — Complete Guide to PHP Type System
In this tutorial, you will learn about PHP Data Types. We cover key concepts, practical examples, and best practices to help you master this topic.
PHP supports eight data types: four scalar (int, float, string, bool), two compound (array, object), and two special (null, resource) that determine how values are stored and manipulated.
What You'll Learn
By the end of this tutorial, you'll understand all PHP data types, type juggling and casting, strict type declarations, Type Checking functions, and when each type is appropriate.
Why PHP Types Matter
Understanding types prevents bugs from unexpected type conversions, improves code clarity with type declarations, and helps you write more reliable PHP applications for platforms like Laravel.
Real-World Use
A payment processing system uses strict types to ensure amounts are always floats, user IDs are integers, and email addresses are strings, preventing calculation errors.
PHP Data Types Learning Path
flowchart LR
A[PHP Basics] --> B[Types]
B --> C[Strings]
C --> D[Numbers]
D --> E[Operators]
A --> F{You Are Here}
style F fill:#f90,color:#fff
Scalar Types
<?php
$age = 25; // int
$price = 19.99; // float
$name = "Alice"; // string
$isActive = true; // bool
var_dump($age, $price, $name, $isActive);
Compound Types
<?php
$colors = ["red", "green", "blue"]; // array
$user = new stdClass(); // object
$user->name = "Alice";
var_dump($colors, $user);
Type Checking Functions
<?php
$value = "42";
var_dump(is_int($value)); // false
var_dump(is_string($value)); // true
var_dump(is_numeric($value)); // true
var_dump(is_array([1, 2])); // true
var_dump(is_null(null)); // true
Type Juggling
<?php
var_dump("10" + 5); // int(15) — string "10" converted to int
var_dump("10" . 5); // string("105") — int 5 converted to string
var_dump(10 + "5 cars"); // int(15) — PHP 8+ warning, returns 15
Strict Types
<?php
declare(strict_types=1);
function add(int $a, int $b): int {
return $a + $b;
}
echo add(5, 10); // 15
// echo add("5", 10); // TypeError in strict mode
Common Mistakes
1. Assuming Loose Comparison Is Safe
"0" == false is true in loose comparison. Use === for type-safe comparison.
2. Forgetting declare(strict_types=1)
Without strict types, PHP silently converts types. Enable strict types in production code.
3. Confusing is_numeric and is_int
is_numeric("42.5") returns true. is_int("42.5") returns false. Use the appropriate function.
4. Relying on Automatic Type Conversion
"10 apples" + 5 produces 15 with a PHP 8+ deprecation warning. Always validate numeric input.
5. Not Checking Types Before Operations
Passing an array to a string function causes a fatal error. Always verify types before operations.
Practice Questions
1. What are the eight PHP data types?
int, float, string, bool (scalar), array, object (compound), null, resource (special).
2. What is the difference between == and ===?
== compares values after type juggling. === compares both value and type without conversion.
3. What does declare(strict_types=1) do?
It enforces strict type checking for function arguments and return types in the current file, throwing TypeError on mismatch.
4. Why does "10" + 5 return 15?
The + operator triggers numeric context, converting "10" to integer 10. The . operator would concatenate as "105".
5. Challenge: Write a function that safely divides two numbers, handling type mismatches and division by zero.
<?php
declare(strict_types=1);
function safeDivide(int|float $a, int|float $b): int|float|string {
if ($b === 0) return "Cannot divide by zero";
return $a / $b;
}
echo safeDivide(10, 3); // 3.333...
echo safeDivide(10, 0); // Cannot divide by zero
FAQ
Mini Project: Type Validator
Create a utility function that validates and sanitizes form inputs by type.
<?php
function sanitizeInput(mixed $value, string $type): int|float|string|bool {
return match($type) {
"int" => filter_var($value, FILTER_VALIDATE_INT) ?: 0,
"float" => filter_var($value, FILTER_VALIDATE_FLOAT) ?: 0.0,
"string" => htmlspecialchars(strip_tags((string)$value)),
"bool" => filter_var($value, FILTER_VALIDATE_BOOLEAN),
default => (string)$value
};
}
echo sanitizeInput("42", "int"); // 42
echo sanitizeInput("<script>alert(1)</script>", "string"); // alert(1)
What's Next
PHP Strings PHP Numbers PHP Arrays
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro