我的代码有一些长常数。为了避免错误,我想声明带有千位分隔符的数字。在 Java 7 中,我可以这样做:
int MyConst = 1_000_000; ///The token '_' (thousand separator) make it more clearance
如何在 PHP 中做到这一点?
PHP 不提供这样的功能。请参阅:http://www.php.net/manual/en/language.types.integer.php
您可以使用科学记数法来表示大数字:http://www.php.net/manual/en/language.types.float.php但是话又说回来,您的整数实际上是浮点数,您可能有添加类型转换。
在 php 中你只需声明
$myVar = 1000000;
来自手册:
整数的大小取决于平台,但通常的最大值约为 20 亿(即 32 位有符号)。 64 位平台的最大值通常约为 9E18。 PHP 不支持无符号整数。从 PHP 4.4.0 和 PHP 5.0.5 开始,可以使用常量 PHP_INT_SIZE 确定整数大小,并使用常量 PHP_INT_MAX 确定最大值。
在 php 中我们这样做
$input = 1000000;
$output = number_format( $input , 0 , '.' , '_' );
echo $output;
输出:1_000_000
此语法是在 7.4 中添加的。从那时起,您可以像在 Java 中一样使用下划线。然而,你不能做的是:
1__6
_16
和 16_
1_.6
或 1._6
1.6_e-28
或 1.6e_-28
0_b01010
或 0b_010101
0_xf00d
或 0x_food