HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Data Types

Written by RedoHub Team · Last updated: August 1, 2026

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.


The var_dump() Function

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)
?>

Supported PHP Data Types

PHP supports the following primary data types:

1. String

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!';
?>

2. Integer

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);
?>

3. Float (Floating Point Numbers)

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);
?>

4. Boolean

A Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional testing.

<?php
    $x = true;
    $y = false;
?>

5. Array

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);
?>

6. Object

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);
?>

7. NULL Value

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);
?>
Resource Type: There is an additional type called a Resource. It is not an actual data type but a reference to a function or resource external to PHP, such as a database connection or a file stream.

Key Points to Remember

  • Use var_dump() to see the data type and value of any variable.
  • Strings must be wrapped in quotes.
  • Integers are whole numbers, while Floats are decimal numbers.
  • Arrays can store multiple values in a single variable.
  • NULL serves as a placeholder for a variable with no value.
  • PHP is loosely typed, so a variable can change its data type depending on the value it currently holds.

Common Mistakes to Avoid

Using == 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.
Trying to 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.
Forgetting PHP is loosely typed. "5" + "10" gives the integer 15, not the string "510" — numeric strings are converted automatically in arithmetic, which surprises anyone coming from JavaScript.

Try It: Comparing var_dump() Output

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
?>

Frequently Asked Questions

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.