[英]Convert number to string in php
在下面我尝试$code = (string)$code;
没有成功,如何在 PHP 中将数字转换为字符串?
$code = 087326487326;
$strlen = strlen($code);
print $strlen."<br/>";
for ($i = $strlen; $i >= 0; $i--) {
print substr($code, 0, $i)."<br/>";
}
输出:
1
0
和
$code = '087326487326';
$strlen = strlen($code);
print $strlen."<br/>";
for ($i = $strlen; $i >= 0; $i--) {
print substr($code, 0, $i)."<br/>";
}
输出:
12
087326487326
08732648732
0873264873
087326487
08732648
0873264
087326
08732
0873
087
08
0
它失败了,因为它以0
为前缀,使 PHP 尝试将其解释为八进制数,其中8
在解析字符串时不是有效的八进制数字,因此您得到0
。
解决方案是使用(string)
强制转换或strval()
,但您需要从$code
的定义中删除前导零。
$code = 87326487326;
var_dump( $code, (string) $code, strval( $code));
这将输出(在 x64 机器上):
int(87326487326) string(11) "87326487326" string(11) "87326487326"
我喜欢这种类型的杂耍方式,如果你有的话。
$code = 087326487326;
将其转换为字符串所需要做的就是:
$code = "$code";
编辑
对不起,有点分心,我测试错了。 上面说的是真的,领先的零是一场灾难。 方式不删除它,然后填充数字?
你可以像这样使用类型转换吗
$code = (string)58963245874;
检查它你打印它的类型如下
echo gettype($code);
您可以使用以下函数转换为字符串:
echo strval($code);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.