Questions And Answers

More Tutorials

PHP Integers

Integers in PHP can be natively specified in base 2 (binary), base 8 (octal), base 10 (decimal), or base 16

$my_decimal = 42;
$my_binary = 0b101010;
$my_octal = 052;
$my_hexadecimal = 0x2a;
echo ($my_binary + $my_octal) / 2;



Integers are 32 or 64 bits long, depending on the platform. The constant PHP_INT_SIZE holds integer size in bytes. PHP_INT_MAX and (since PHP 7.0) PHP_INT_MIN are also available.

printf("Integers are %d bits long" . PHP_EOL, PHP_INT_SIZE * 8);
printf("They go up to %d" . PHP_EOL, PHP_INT_MAX);

Integer values are automatically created as needed from floats, booleans, and strings. If an explicit typecast is needed, it can be done with the (int) or (integer) cast:

$my_numeric_string = "123";
$my_integer = (int)$my_numeric_string;


string(3) "123"

Integer overflow will be handled by conversion to a float:

$too_big_integer = PHP_INT_MAX + 7;



There is no integer division operator in PHP, but it can be simulated using an implicit cast, which always 'rounds' by just discarding the float-part. As of PHP version 7, an integer division function was added.

$not_an_integer = 25 / 4;
var_dump((int) (25 / 4));
var_dump(intdiv(25 / 4));



(Note that the extra parentheses around (25 / 4) are needed because the (int) cast has higher precedence than the division)


In this page (written and validated by ) you learned about PHP Integers . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Operators.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.