Php 简明教程
PHP - Integers
Integer 是 PHP 中内置标量类型之一。在字面量中没有小数点的整数在 PHP 中为“int”类型。整数可以用十进制(基数 10)、十六进制(基数 16)、八进制(基数 8)或二进制(基数 2)记法表示。
Integer is one of the built-in scalar types in PHP. A whole number, without a decimal point in the literal, is of the type "int" in PHP. An integer can be represented in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation.
要使用八进制记法,数字之前加上“0o”或“0O”(PHP 8.1.0 及更早版本)。从 PHP 8.1.0 开始,以“0”为前缀且没有小数点的数字为八进制数字。
To use octal notation, a number is preceded with "0o" or "0O" (PHP 8.1.0 and earlier). From PHP 8.1.0 onwards, a number prefixed with "0" and without a decimal point is an octal number.
要使用十六进制记法,数字之前加上“0x”。要使用二进制记法,数字之前加上“0b”。
To use hexadecimal notation, precede the number with "0x". To use binary notation, precede the number with "0b".
Example
请查看以下示例:
Take a look at this following example −
<?php
$a = 1234;
echo "1234 is an Integer in decimal notation: $a\n";
$b = 0123;
echo "0o123 is an integer in Octal notation: $b\n";
$c = 0x1A;
echo "0xaA is an integer in Hexadecimal notation: $c\n";
$d = 0b1111;
echo "0b1111 is an integer in binary notation: $d";
?>
它将生成以下 output −
It will produce the following output −
1234 is an Integer in decimal notation: 1234
0o123 is an integer in Octal notation: 83
0xaA is an integer in Hexadecimal notation: 26
0b1111 is an integer in binary notation: 15
从 PHP 7.4.0 开始,整数字面量可能包含下划线 (_) 作为数字之间的分隔符,以提高字面量的可读性。这些下划线由 PHP 的扫描器移除。
PHP 7.4.0 onwards, integer literals may contain underscores (_) as separators between digits, for better readability of literals. These underscores are removed by PHP’s scanner.
Example
请查看以下示例:
Take a look at this following example −
<?php
$a = 1_234_567;
echo "1_234_567 is an Integer with _ as separator: $a";
?>
它将生成以下 output −
It will produce the following output −
1_234_567 is an Integer with _ as separator: 1234567
PHP does not support unsigned ints 。 int 的大小依赖于平台。在 32 位系统上,最大值约为二十亿。64 位平台的最大值通常约为 9E18。
PHP does not support unsigned ints. The size of an int is platform dependent. On 32 bit systems, the maximum value is about two billion. 64-bit platforms usually have a maximum value of about 9E18.
int 大小可以用常量 PHP_INT_SIZE 确定,最大值可以用常量 PHP_INT_MAX 确定,最小值可以用常量 PHP_INT_MIN 确定。
int size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX, and minimum value using the constant PHP_INT_MIN.
如果整数恰好超出了 int 类型的边界,或任何操作导致数字超出了 int 类型的边界,它将被解释为浮点数。
If an integer number happens to be beyond the bounds of the int type, or any operation results in a number beyond the bounds of the int type, it will be interpreted as a float instead.
Example
请查看以下示例:
Take a look at this following example −
<?php
$x = 1000000;
$y = 50000000000000 * $x;
var_dump($y);
?>
它将生成以下 output −
It will produce the following output −
float(5.0E+19)
PHP 没有用于整数除法的运算符。因此,整数和浮点数之间的除法运算总是导致浮点数。要获得整数除法,可以使用 intval() 内置函数。
PHP doesn’t have any operator for integer division. Hence, a division operation between an integer and a float always results in float. To obtain integral division, you may use the intval() built-in function.