PHP — Data Types
Eight data types
PHP has eight data types, grouped into three categories:
| Category | Types |
|---|---|
| Scalar | int, float, string, bool |
| Compound | array, object |
| Special | resource, NULL |
Integer
Whole numbers without a decimal point:
<?php
$a = 42;
$b = -100;
$c = 0;
$d = 1_000_000; // Underscores for readability (PHP 7.4+)
echo gettype($a); // integer
?>
Integers are stored as 64-bit values on most platforms, supporting values up to about 9 quintillion.
Float
Numbers with a decimal point (also called doubles):
<?php
$pi = 3.14159;
$scientific = 1.5e10; // 15,000,000,000
$hex = 0x1A; // 26 (PHP auto-converts)
echo gettype($pi); // double
?>
Warning: Floats can be imprecise due to binary representation:
<?php
echo 0.1 + 0.2; // 0.30000000000000004
// For exact decimals, use:
echo bcadd("0.1", "0.2", 10); // 0.3
?>
String
Text enclosed in single or double quotes:
<?php
$single = 'Hello, world!'; // Single-quoted (literal)
$double = "Hello, world!"; // Double-quoted (interpolation)
$backtick = `ls -la`; // Execution operator (runs command)
echo "My name is $single"; // Interpolation works in double quotes
echo 'My name is $single'; // Literal: My name is $single
?>
Escape sequences in double-quoted strings:
<?php
echo "Line 1\nLine 2"; // Newline
echo "Tab\there"; // Tab
echo "Quote: \"Hi\""; // Escaped quotes
echo "Backslash: \\"; // Escaped backslash
?>
Boolean
true or false:
<?php
$isActive = true;
$isDeleted = false;
// Many values are "falsy" in PHP:
// false, 0, 0.0, "", "0", null, empty array, empty object
var_dump((bool) 0); // false
var_dump((bool) 1); // true
var_dump((bool) ""); // false
var_dump((bool) "abc"); // true
?>
Array
An ordered map of key-value pairs:
<?php
// Indexed array
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0]; // Apple
// Associative array
$person = [
"name" => "Alice",
"age" => 30,
"email" => "alice@example.com"
];
echo $person["name"]; // Alice
// Mixed
$mixed = [1, "two", 3.0, true];
?>
Object
An instance of a class:
<?php
class Person {
public string $name;
public int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
public function greet(): string {
return "Hi, I'm {$this->name}!";
}
}
$alice = new Person("Alice", 30);
echo $alice->greet(); // Hi, I'm Alice!
echo gettype($alice); // object
?>
NULL
The absence of a value:
<?php
$var = null; // Explicit NULL
$other = unset($var); // unset returns void
var_dump($var); // NULL
// NULL is falsy
if (!$var) {
echo "This variable is NULL";
}
// isset() returns false for NULL
echo isset($var); // 0 (false)
echo is_null($var); // 1 (true)
?>
Resource
A reference to an external resource (file, database connection, etc.):
<?php
$file = fopen("example.txt", "r"); // resource
echo get_resource_id($file); // Unique ID
fclose($file); // Free the resource
?>
Resources are not created directly — they come from built-in functions like fopen(), mysqli_connect(), or curl_init().
Type casting
Convert between types using explicit casts:
<?php
$x = "42";
$y = (int) $x; // 42 (integer)
$z = (float) $x; // 42.0 (float)
$b = (bool) $x; // true (non-empty string)
echo gettype($y); // integer
echo gettype($z); // double
echo gettype($b); // boolean
?>
PHP also provides safe casting functions:
<?php
intval("123abc"); // 123
floatval("3.14abc"); // 3.14
strval(42); // "42"
boolval(0); // false
settype($var, "int"); // Convert in place
?>
Type checking functions
| Function | Returns true if |
|---|---|
is_int($v) | Value is an integer |
is_float($v) | Value is a float |
is_string($v) | Value is a string |
is_bool($v) | Value is a boolean |
is_array($v) | Value is an array |
is_object($v) | Value is an object |
is_null($v) | Value is NULL |
is_resource($v) | Value is a resource |
Mini Practice
Write PHP code that:
- Creates variables of each scalar type and prints their types
- Demonstrates the difference between single and double quoted strings
- Creates an associative array and accesses its values
- Uses type casting to convert a string to an integer and a float
Up Next
In the next lesson, you'll learn about Strings — formatting, manipulation, and common string functions.
Related Topics
Frequently Asked Questions about Data Types
What is Data Types in PHP?
Data Types is a fundamental concept in PHP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Data Types?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.
Why is Data Types important in PHP?
Data Types is essential for PHP development. Understanding this concept will help you write better code and solve real-world problems more effectively.