PHP Data Types Explained with Examples in PHP 8

PHP data types are one of those topics that look simple when you first learn them. You see a list of types, remember a few names, and move on. Then a few months later, a strange bug appears because a value was a string when you expected an integer. It happens more often than developers like to admit.

Understanding PHP data types helps you write predictable code. It becomes especially important when working with function parameters, return values, class properties, API responses, and database values.

PHP is a dynamically typed language, which means you usually do not need to declare the type of a variable when creating it. PHP determines the type based on the assigned value. However, modern PHP versions also support type declarations, allowing you to make your code safer and easier to maintain.

Quick Answer: What are PHP data types?

PHP data types define the kind of value a variable can store. PHP has eight commonly used built-in data types:

  • String – stores text values.
  • Integer – stores whole numbers.
  • Float – stores decimal numbers.
  • Boolean – stores true or false values.
  • Array – stores multiple values.
  • Object – stores objects created from classes.
  • Resource – stores references to external resources.
  • NULL – represents a variable with no value.

These types are grouped into scalar, compound, and special data types. The complete list and behaviour of PHP types are documented in the official PHP type system documentation.

PHP Data Types Classification

Category Data Types Description
Scalar String, Integer, Float, Boolean Stores a single value.
Compound Array, Object Stores collections or complex data.
Special Resource, NULL Used for special cases.

Scalar Data Types in PHP

Scalar data types store a single value. They are the most commonly used types in everyday PHP development.

1. String

A string stores a sequence of characters. Text values are usually enclosed inside single or double quotes.

<?php
$name = "John Doe";

echo $name;
?>

Double quoted strings support variable interpolation, which means PHP can replace variables directly inside the string.

<?php
$name = "John";

echo "Hello $name";
?>

2. Integer

An integer is a whole number without decimal places. Integers can be positive or negative.

<?php
$age = 25;

var_dump($age);
?>

Output:

int(25)

3. Float

A float stores numbers with decimal values. It is commonly used for values such as measurements, percentages, and calculations involving fractions.

<?php
$price = 19.99;

var_dump($price);
?>

Output:

float(19.99)

4. Boolean

A Boolean represents one of two values: true or false. It is commonly used in conditions and status checks.

<?php
$isLoggedIn = true;

var_dump($isLoggedIn);
?>

Output:

bool(true)

Compound Data Types in PHP

Compound data types can store multiple values or more complex structures. They are useful when handling collections of data or creating reusable application logic.

5. Array

An array stores multiple values in a single variable. PHP arrays are flexible and can store different data types together.

<?php
$colors = ["Red", "Green", "Blue"];

var_dump($colors);
?>

PHP supports indexed arrays, associative arrays, and multidimensional arrays.

An associative array uses named keys instead of numeric indexes.

<?php
$user = [
    "name" => "John",
    "email" => "john@example.com"
];

echo $user["name"];
?>

6. Object

An object is an instance of a class. Objects allow developers to group data and related methods together using object-oriented programming.

<?php
class User
{
    public string $name = "John";
}

$user = new User();

echo $user->name;
?>

Objects are commonly used in modern PHP applications, especially when working with frameworks and larger codebases. Understanding PHP object-oriented programming concepts helps when building applications using classes and objects.

Special Data Types in PHP

Special data types are used for specific situations where normal values are not enough.

7. NULL

The NULL data type represents a variable that has no value assigned to it.

<?php
$value = null;

var_dump($value);
?>

Output:

NULL

A variable becomes NULL when it is explicitly assigned null, when it is declared without a value, or when it is removed using unset().

8. Resource

A resource is a special variable that holds a reference to an external resource. Examples include file handles and database connections.

<?php
$file = fopen("example.txt", "r");

var_dump($file);
?>

In modern PHP applications, resources are less visible because many libraries handle them internally. However, you may still encounter resources when working with file operations or low-level PHP functions.

Checking Data Types in PHP

PHP provides several built-in functions to check the type of a variable. The most commonly used function is var_dump(). PHP also provides several variable handling functions to inspect and work with variable values and types. You can also refer to the official var_dump() documentation for details about its output format.

<?php
$value = "PHP";

var_dump($value);
?>

Output:

string(3) "PHP"

You can also use specific type checking functions when you need to validate a value before processing it.

  • is_string() checks whether a value is a string.
  • is_int() checks whether a value is an integer.
  • is_float() checks whether a value is a float.
  • is_bool() checks whether a value is a Boolean.
  • is_array() checks whether a value is an array.
  • is_object() checks whether a value is an object.
  • is_null() checks whether a value is NULL.

Example:

<?php
$email = "user@example.com";

if (is_string($email)) {
    echo "The value is a string";
}
?>

Type Declarations in PHP

Although PHP is dynamically typed, modern PHP versions allow developers to declare expected types for function arguments, return values, and class properties.You can learn more about PHP type declarations and type hinting in a separate guide.

Type declarations help catch mistakes earlier and make code easier to understand.

Function Parameter Type Declaration

<?php
function calculateTotal(float $price, int $quantity): float
{
    return $price * $quantity;
}

echo calculateTotal(10.5, 2);
?>

The function above expects a float value for $price, an integer value for $quantity, and returns a float value.

Property Type Declaration

<?php
class Product
{
    public string $name;
    public float $price;
}

$product = new Product();

$product->name = "Laptop";
$product->price = 899.99;
?>

Using type declarations is not mandatory in PHP, but it is a good practice for maintainable applications, especially as projects grow. The official PHP type declarations documentation covers supported declarations and how PHP handles them.

Type Juggling in PHP

One feature that surprises developers coming from strongly typed languages is PHP’s automatic type conversion, also called type juggling.

PHP can automatically convert a value from one data type to another depending on the operation being performed. This behaviour is called type juggling in PHP documentation.

For example, when a numeric string is used in an arithmetic operation, PHP converts it into a number.

<?php
$value = "100";

$result = $value + 50;

var_dump($result);
?>

Output:

int(150)

This flexibility is convenient, but it can also create unexpected results when input values are not validated properly. Using type declarations is one way to make expected values clearer in larger applications.

For example, values received from forms are always strings, even when users enter numbers.

<?php
$quantity = $_POST["quantity"];

var_dump($quantity);
?>

If the application expects a number, validate and convert the value before using it.

<?php
$quantity = (int) $_POST["quantity"];

var_dump($quantity);
?>

Strict Type Checking in PHP

PHP allows you to enable strict typing using the declare() statement. This makes PHP enforce scalar type declarations more strictly.

<?php
declare(strict_types=1);

function add(int $a, int $b): int
{
    return $a + $b;
}

echo add(10, "20");
?>

With strict typing enabled, passing a string instead of an integer causes a TypeError.

Without strict typing, PHP may attempt to convert compatible values automatically. The behaviour can be convenient for small scripts, but strict typing is often preferred in larger applications because it makes errors easier to find.

Common PHP Data Type Mistakes

Comparing Values Without Checking Types

PHP has both loose comparison (==) and strict comparison (===). Loose comparison allows PHP to convert types before comparing values.

<?php
var_dump(0 == "hello");
var_dump(0 === "hello");
?>

Using strict comparison with === is usually safer because it checks both the value and the data type.

Assuming Form Input Has the Correct Type

Values from HTML forms, query strings, and cookies are received as strings. Do not assume a number entered by a user is automatically an integer.

<?php
$age = $_GET["age"];

if (is_numeric($age)) {
    $age = (int) $age;
}
?>

Using Empty Values Without Understanding NULL

PHP has several ways to represent an empty-looking value. An empty string, zero, false, and NULL are different values.

<?php
var_dump("");
var_dump(0);
var_dump(false);
var_dump(null);
?>

Understanding these differences prevents many conditional logic bugs.

PHP Data Types FAQ

Is PHP strongly typed or weakly typed?

PHP is generally considered a dynamically typed and weakly typed language because it allows automatic type conversion. However, PHP supports optional type declarations and strict typing for developers who want stronger type checking.

How can I find the data type of a variable in PHP?

Use var_dump() to display the value and its data type.

<?php
$value = 123;

var_dump($value);
?>

What is the default data type in PHP?

PHP does not assign a default data type to variables. The type is determined automatically from the value assigned to the variable.

Can a PHP variable change its data type?

Yes. PHP variables are not locked to a specific type, so the same variable can store different types of values during execution.

<?php
$value = 100;
$value = "PHP";

var_dump($value);
?>

Conclusion

PHP data types are simple on the surface, but understanding how PHP handles values is important when building reliable applications.

Knowing the difference between strings, numbers, arrays, objects, and special types helps you avoid subtle bugs. Modern PHP features such as type declarations and strict typing give developers more control while keeping the language flexible.

Photo of Vincy, PHP developer
Written by Vincy Last updated: August 14, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?