bhylu.com

Advertisements

PHP Data Types

An integer count, a numeric-looking string, a boolean flag and null carry different meanings. Inspect those meanings before using a value in a calculation or condition.

How it works

PHP has scalar types, compound types such as arrays and objects, and special cases such as resources and null. get_debug_type is useful for readable diagnostic type names. A form field normally arrives as text, even when the visitor typed digits. Strict type declarations help at function boundaries, but a raw request still needs parsing and validation. Do not expose a full diagnostic dump of private application values to public visitors.

Run it locally

Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.

php -l types.php
php types.php

Download this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.

Example

PHP 8.2+ · types.php

<?php
declare(strict_types=1);

$values = [12, "12", true, null, ["Spade"]];
foreach ($values as $value) {
    echo get_debug_type($value), "\n";
}

Expected result

int
string
bool
null
array

The first two values print different type names despite their similar appearance. null denotes the deliberate absence of a value here. The final array groups values rather than representing one scalar field. This type list is useful while developing a parser, but it does not decide whether an input is a valid lending duration or an authorised action.

Check the official PHP manual for detailed version and API contracts.