Variables can store data of different types, and different data types can do different things. PHP supports several data types that allow you to work with text, numbers, logic, and complex collections of data.
Before we dive into types, you must know about var_dump(). It is a built-in PHP function that returns the data type and value of a variable. It is the most useful tool for debugging and learning data types.
<?php
$x = 5;
var_dump($x); // Outputs: int(5)
?>
PHP supports the following primary data types:
A string is a sequence of characters, like "Hello world!". A string can be any text inside quotes. You can use single or double quotes.
<?php
$x = "Hello world!";
$y = 'Hello world!';
?>
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. An integer must have at least one digit and cannot contain commas or blanks.
<?php
$x = 5985;
var_dump($x);
?>
A float (floating point number) is a number with a decimal point or a number in exponential form. In some documentation, it is also referred to as a double.
<?php
$x = 10.365;
var_dump($x);
?>
A Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional testing.
<?php
$x = true;
$y = false;
?>
An array stores multiple values in one single variable. It is a collection of data, and can store values of different types within the same array.
<?php
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
?>
Classes and objects are the two main aspects of object-oriented programming. An object is a data type which stores data and information on how to process that data with methods.
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
}
$myCar = new Car("black", "Volvo");
var_dump($myCar);
?>
Null is a special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it.
<?php
$x = "Hello world!";
$x = null; // $x is now NULL
var_dump($x);
?>
== instead of === across types. "0" == false and "abc" == 0 can both evaluate to true in older PHP versions due to type juggling — === compares type as well as value and avoids the surprise.
echo an array or object directly. echo $cars; just prints the word "Array" (with a notice) — use print_r() or var_dump() to actually see the contents.
"5" + "10" gives the integer 15, not the string "510" — numeric strings are converted automatically in arithmetic, which surprises anyone coming from JavaScript.
Run this to see exactly how PHP reports each type — useful whenever a bug turns out to be a type mismatch:
<?php
var_dump("5"); // string(1) "5"
var_dump(5); // int(5)
var_dump("5" == 5); // bool(true) -- loose comparison
var_dump("5" === 5); // bool(false) -- strict comparison
?>
Q: How do I check the type of a variable?
A: Use gettype($var) for a type name as a string, or var_dump($var) to see the type and value together during debugging.
Q: What's the difference between var_dump and print_r?
A: var_dump() shows the data type alongside the value (useful for debugging type issues); print_r() shows just a human-readable value, which is easier to read but hides the type.
Q: Is PHP a strongly typed language?
A: No — it's dynamically and loosely typed by default, though PHP 7+ lets you opt into stricter type checking with type declarations and declare(strict_types=1). See the PHP manual's types guide for details.