Variables in PHP can store different types of data, and different data types perform different operations. For example, you can perform arithmetic multiplication on integers and floats, but not on strings. PHP automatically determines the data type of a variable upon assignment. In this comprehensive guide, we explore all 8 primary PHP data types, inspection utilities like var_dump(), explicit type casting, and modern PHP 8 strict type declarations.
Figure 2: Complete taxonomy of PHP's Scalar, Compound, and Special Data Types.
$name = "Veewom";$age = 21;$price = 199.95;true or false (case-insensitive): $isActive = true;$languages = ["PHP", "Python", "JavaScript"];$user = new Student();null. It signifies that a variable has no value or has been emptied.$handle = fopen("data.txt", "r");var_dump() vs gettype()When debugging code or grading student assignments, two built-in functions provide immediate inspection of any variable:
<?php
$studentName = "Deepak Verma";
$marks = 94;
$gpa = 3.88;
$isPassed = true;
$hobbies = ["Coding", "Cricket", "Blogging"];
$certifications = null;
// gettype() returns just the type name string
echo gettype($studentName) . "<br>"; // string
// var_dump() outputs type, size, and structure
var_dump($studentName); // string(12) "Deepak Verma"
echo "<br>";
var_dump($marks); // int(94)
echo "<br>";
var_dump($gpa); // float(3.88)
echo "<br>";
var_dump($isPassed); // bool(true)
echo "<br>";
var_dump($hobbies); // array(3) { [0]=> string(6) "Coding" ... }
echo "<br>";
var_dump($certifications); // NULL
?>
You can explicitly convert a variable from one data type to another using casting operators:
(int) or (integer) — Casts to Integer(float) or (double) — Casts to Float(string) — Casts to String(bool) or (boolean) — Casts to Boolean(array) — Casts to Array<?php
$priceStr = "149.99";
$intPrice = (int)$priceStr; // 149 (truncates decimals)
$floatPrice = (float)$priceStr; // 149.99
$counter = 0;
$isTrue = (bool)$counter; // false (0 evaluates to false in boolean cast)
?>
By default, PHP coerces data types automatically (e.g. passing numeric string "25" into a function expecting int works seamlessly). However, for mission-critical software, PHP supports strict typing:
<?php
declare(strict_types=1); // MUST be the very first statement in the file
function calculateArea(int $width, int $height): int {
return $width * $height;
}
echo calculateArea(10, 20); // Valid: outputs 200
// calculateArea("10", 20);
// Fatal Error: Uncaught TypeError: calculateArea(): Argument #1 ($width) must be of type int, string given!
?>
Semester Exam Question:
Q: What are falsy values in PHP?
A: When cast to boolean, the following values evaluate to false:
The boolean false, integer 0, float 0.0, empty string "", string zero "0", empty array [], and null.
Exercise 6.1: Declare variables representing an item in an online bookstore: title (string), ISBN (string), price (float), inStock (boolean), tags (array). Use var_dump() to inspect all five.
Exercise 6.2: Write a script with declare(strict_types=1); that defines a function getDiscount(float $price, float $percent): float and test valid and invalid argument types.