PHP Data Types

A data type describes what kind of value a variable holds and what operations PHP will allow on it.

What data types are in PHP

Every value in PHP belongs to a type, and that type controls how the value behaves in comparisons, arithmetic, and function calls. PHP is a dynamically typed language, which means you never declare the type of a variable up front. Instead, the type is decided at runtime by whatever value you assign, and it can change the moment you assign a different value. This flexibility is convenient, but it also means you should stay aware of what type a variable actually holds at any given point.

  • String — a sequence of characters, such as "hello".
  • Integer — a whole number without a decimal point, such as 42 or -7.
  • Float — a number with a decimal point, such as 3.14 (also called double).
  • Boolean — one of two logical values, true or false.
  • Array — an ordered map that can hold many values under one variable.
  • Object — an instance of a class, bundling data and behaviour together.
  • NULL — a special type whose only value is null, meaning 'no value'.
  • Resource — a reference to an external resource such as an open file or database link.

Assigning values of different types

<?php
$name = "Ada";      // string
$age = 36;           // integer
$height = 1.68;      // float
$isMember = true;    // boolean
$scores = [90, 85, 78]; // array
$note = null;        // NULL

echo $name . " is " . $age . " years old.";
?>

Checking a value's type

Because types are decided at runtime, PHP gives you tools to inspect them. The var_dump() function prints both the type and the value, which makes it ideal for debugging. The gettype() function returns the type name as a string, and there is a family of is_* functions such as is_int(), is_string(), and is_array() that return a boolean so you can branch on the result.

Inspecting types with var_dump and is_* functions

<?php
$price = 19.99;
$quantity = 3;

var_dump($price);        // float(19.99)
var_dump($quantity);     // int(3)

if (is_float($price)) {
    echo "Price is a float";
}
?>
Note: var_dump() is the fastest way to see exactly what PHP thinks a variable is. When a comparison behaves unexpectedly, dumping the values often reveals that one of them is a string where you expected a number.

Type juggling and conversion

PHP will often convert a value from one type to another automatically, a behaviour known as type juggling. For example, the numeric string "10" becomes the integer 10 when used in arithmetic. You can also force a conversion yourself with a cast, writing the target type in parentheses before the value, such as (int) or (string). Explicit casts make your intent clear and help avoid surprises.

Automatic juggling versus explicit casting

<?php
$result = "10" + 5;      // string juggled to int, result is 15
echo $result;            // 15

$number = (int) "42abc"; // explicit cast, result is 42
echo $number;            // 42

$text = (string) 3.5;    // explicit cast to string
var_dump($text);         // string(3) "3.5"
?>
Note: Type juggling can hide bugs. Relying on PHP to guess your intent may produce results you did not expect, so prefer explicit casts and strict comparisons when the exact type matters.

Exercise: PHP Data Types

Which built-in function reports the data type of a variable's current value?