Data Types

Scalar vs compound types, strictness, and how types behave in real code.

On this page

Scalar Types

Scalar types represent single values: int, float, string, bool.

<?php
$qty = 3;               // int
$ratio = 0.75;          // float
$title = 'PHP';       // string
$isOk = false;          // bool

Compound Types

Compound types include array and object. They hold structured data.

<?php
$tags = ['php', 'mysql'];               // array
$user = (object)['id' => 1, 'name' => 'Ozan']; // object

Null

null means “no value.” Many bugs come from assuming something is always a string or always an array.

<?php
$maybe = null;

Type Casting

Explicit casting is common for request data (everything from requests is string-like). Cast carefully.

<?php
$page = (int)($_GET['page'] ?? 1);
$enabled = (bool)($_GET['enabled'] ?? false);

Type Declarations in Functions

Type hints improve reliability and readability. Use them especially in “production-minded” code.

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