You get a bonus - 1 coin for daily activity. Now you have 1 coin

Integers in PHP

Lecture



An integer is a number from the set Z = {..., -2, -1, 0, 1, 2, ...}, usually 32 bits long (from –2,147,483,648 to 2,147,483,647).

Integers can be specified in decimal, hexadecimal or octal notation, optionally, with a preceding (- or +) sign.

If you are using the octal number system, you must precede the number 0 (zero), to use the hexadecimal system, you must put it in front of the number 0x .

<?php
$a = 1234; // десятичное число
$a = -123; // отрицательное число
$a = 0123; // восьмеричное число (эквивалентно 83 в десятичной системе)
$a = 0x1A; // шестнадцатеричное число (эквивалентно 26 в десятичной системе)
?>

Formally, the possible structure of integers is:

десятичные : [1-9][0-9]*
| 0

шестнадцатеричные : 0[xX][0-9a-fA-F]+

восьмеричные : 0[0-7]+

целые:

[+-]?десятичные

| [+-]?шестнадцатеричные

| [+-]?восьмеричные

The size of the whole depends on the platform, although, as a rule, the maximum value is about two billion (this is a 32-bit sign). PHP does not support unsigned integers.

Excess of the size of the whole

If you define a number that exceeds the limits of the integer type, it will be interpreted as a floating point number. Also, if you use an operator whose result is a number that exceeds the integer limits, a floating point number will be returned instead.

<?php
$large_number = 2147483647;
var_dump($large_number);
// вывод: int(2147483647)

$large_number = 2147483648;
var_dump($large_number);
// вывод: float(2147483648)

// это справедливо и для шестнадцатеричных целых:
var_dump( 0x80000000 );
// вывод: float(2147483648)

$million = 1000000;
$large_number = 50000 * $million;
var_dump($large_number);
// вывод: float(50000000000)
?>

There is no integer division operator in PHP. The result 1/2 will be a floating point number 0.5 . You can cast a value to a whole, which always rounds it down, or use the round () function.

<?php
var_dump(25/7); // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // float(4)
?>

Integer conversion

To undoubtedly convert a value to an integer, use a cast (int) or (integer) . However, in most cases you do not need to use a cast, since the value will be automatically converted if an operator, function, or control construct requires an integer argument. You can also convert a value to an integer using the intval () function.

Conversion from integer to boolean

FALSE is converted to 0 (zero), and TRUE is converted to 1 (one).

Convert type integer to type float

When converting from a floating point number to an integer, the number will be rounded to zero .

If the floating-point number exceeds the limits of the integer (as a rule, it is +/- 2.15e + 9 = 2 ^ 31 ), the result will be undefined, since the integer does not have sufficient accuracy to return the correct result. In this case, neither a warning nor even a comment will be displayed!

Attention! Never give an unknown fraction to a whole, as it can sometimes give unexpected results (more about this feature here) An example of an erroneous conversion:

<?php
echo (int) ( (0.1+0.7) * 10 ); // выводит 7! (ошибка!)
?>

Convert integer type to string type

See: Converting Strings to Numbers

Conversion of integer type to other data types

For other types, the integer type conversion behavior is undefined.

created: 2016-01-25
updated: 2021-03-13
229



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)