006b Data Types: Integers
An integer is any whole number within a certain range. The range depends on if your PHP is built with a 32-bit platform or a 64-bit platform. There are three helpful constants that show the size and range your integer can be.
<?php
// 32 bit
echo PHP_INT_SIZE; // 4 (bytes)
echo PHP_INT_MAX; // 2147483647
echo PHP_INT_MIN; // -2147483648
// 64 bit
echo PHP_INT_SIZE; // 8 (bytes)
echo PHP_INT_MAX; // 9223372036854775807
echo PHP_INT_MIN; // -9223372036854775808
?>
Your results may vary depending on your system and your PHP build. Either way, it is important to understand the range of integers you are dealing with.
So what would happen if you tried to assign an integer that is bigger (or smaller) than the accepted range?
<?php
$a = PHP_INT_MAX;
var_dump($a); // int
$a = $a + 1;
var_dump($a); // double (float)
$b = PHP_INT_MAX + PHP_INT_MAX;
var_dump($b); // double (float)
?>
Integer notation
Integers can be represented as decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation.
Decimal numbers are expressed as you would normally. For hex, prefix the number with an 0x; for octal, prefix with a 0 or a 0o; for binary, prefix with 0b. Also helpful, you can use underscores to help keep long numbers easily readable. Here is an example from the official documentation on integers:
<?php
$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0o123; // octal number
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (PHP ignores the underscores)
?>
Converting to an integer
To convert to an integer, simply place (int)
before what you want to convert. There are situations that PHP will automatically cast to an integer if an operator, function, or control structure requires an integer. You can also use the intval() method, which has the benefit of allowing you to specify the base of the number it will convert to.
Casting from boolean to int
If you (int) false
, it will convert to 0. If you (int) true
, it will convert to 1.
Casting from float (floating point numbers) to int
Starting PHP version 8.1.0, casting a float to an int is deprecated. It will work, but it will also emit a warning: "Deprecated: Implicit conversion from float to int loses precision." As the warning indicates, when you cast a float, the number is rounded towards 0. So if you were to do (int) 7.9
, you would get 7
. And if you were to do (int) -7.9
, you would get -7
.
The issue is slightly different when you cast a float that is greater or lesser than the range an int supports. In that case, the result is undefined, but PHP won't issue an error or a warning, the number will wrap around and you will get a result that doesn't make sense.
Casting from string to int
This only works if the beginning of the string is a number.
<?php
echo (int) "100"; // 100
echo (int) "1.2"; // 1
echo (int) "abc 123"; // 0
echo (int) "123 abc"; // 123
?>
Casting from NULL to int
Null will always cast to 0.
Casting from other types
Casting to int from other types is not supported, and output will vary. Do not do this.
Resources
Challenges
Find your integer size
Use the constants PHP_INT_SIZE
, PHP_INT_MAX
, and PHP_INT_MIN
to find out the size and range of integers on your system.
Strange casting
Cast from different types to int
. In particular, try casting floats, numbers outside of the integer range your computer supports, and other types like arrays or objects to int
to see what happens.