integer

integer — Is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}.

Can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation.

Integer literals

<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
?>

Integer overflow

<?php
$large_number = 2147483647;
var_dump($large_number);                     // int(2147483647)

$large_number = 2147483648;
var_dump($large_number);                     // float(2147483648)

$million = 1000000;
$large_number =  50000 * $million;
var_dump($large_number);                     // float(50000000000)
?>
<?php
$large_number = 9223372036854775807;
var_dump($large_number);                     // int(9223372036854775807)

$large_number = 9223372036854775808;
var_dump($large_number);                     // float(9.2233720368548E+18)

$million = 1000000;
$large_number =  50000000000000 * $million;
var_dump($large_number);                     // float(5.0E+19)
?>

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead.

An operation which results in a number beyond the bounds of the integer type will return a float instead.

Converting to integer

  • To explicitly convert a value to integer, use either the (int) or (integer) casts.
  • In most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument.
  • A value can also be converted to integer with the intval() function.
  • If a resource is converted to an integer, then the result will be the unique resource number assigned to the resource by PHP at runtime.
  • FALSE will yield 0 (zero), and TRUE will yield 1 (one).
  • When converting from float to integer, the number will be rounded towards zero.
  • NULL is always converted to zero (0).
  • The behaviour of converting to integer is undefined for other types. Do not rely on any observed behaviour, as it can change without notice.

integer — Structure map

Clickable & Draggable!

integer — Related pages: