Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Check When A String Is An Integer

<?php

$string = str_repeat('123', 8); // Beyond PHP_INT_MAX

var_dump(is_numeric($string)); // true 
var_dump(is_int($string)); // false it is a string 
var_dump(is_int(intval($string))); // true
var_dump((int) $string); // PHP_INT_MAX
var_dump((int) $string == (float) $string); // PHP_INT_MAX

Converting a string to an integer is a classic operation. It is still quite suprising.

is_numeric() checks if the the string contains only digits, decimal separator, exponent and +/-. It might be an integer, but also a float.

Then, is_int() checks if the type of the variable is already an integer: it does not check the content, unlike is_numeric().

Then, it is possible to cast the string directly to integer: any integer beyond PHP_INT_MAX is actually capped at PHP_INT_MAX, without a warning. This operation never fails, and might go on with an unexpected value.

Finally, one approach is to cast to integer, and compare to PHP_INT_MAX or to the float value. At least, here, the overflow of floats leads to INF.

See Also

PHP Features

Last updated: 11 October 2026