在 PHP 中将整数转换为字符串
PHP 中有没有办法将整数转换为字符串?
Is there a way to convert an integer to a string in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
PHP 中有没有办法将整数转换为字符串?
Is there a way to convert an integer to a string in PHP?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
您可以使用句点运算符并将一个字符串连接到它(它将被类型转换为字符串):
或者,更正确地说,您可以将整数类型转换为字符串:
You can either use the period operator and concatenate a string to it (and it will be type casted to a string):
Or, more correctly, you can just type cast the integer to a string:
正如这里的答案很好地表明的那样,是的,有多种方法。 然而,在 PHP 中,您实际上很少需要这样做。 编写 PHP 的“教条方式”是依赖该语言的松散类型系统,该系统将根据需要透明地强制类型。 对于整数值,这通常没有问题。 不过,您应该非常小心浮点值。
As the answers here demonstrates nicely, yes, there are several ways. However, in PHP you rarely actually need to do that. The "dogmatic way" to write PHP is to rely on the language's loose typing system, which will transparently coerce the type as needed. For integer values, this is usually without trouble. You should be very careful with floating point values, though.
我想说这取决于上下文。 可以使用 strval() 或转换运算符(字符串)。 然而,在大多数情况下,PHP 会决定什么对你有好处,例如,你将它与 echo 或 printf 一起使用......
一个小注意事项:die() 需要一个字符串并且不会显示任何 int :)
I would say it depends on the context. strval() or the casting operator (string) could be used. However, in most cases PHP will decide what's good for you if, for example, you use it with echo or printf...
One small note: die() needs a string and won't show any int :)
所以回显将返回字符串。
So the echo will be return string.
我的情况:
我正在工作...
My situation :
I'm working ...
我尝试了上面的所有方法,但当我将值嵌入到另一个字符串中时,出现“数组到字符串转换”错误。 如果您遇到与我同样的问题,请尝试 implode() 函数。
例子:
I tried all the methods above yet I got "array to string conversion" error when I embedded the value in another string. If you have the same problem with me try the implode() function.
example:
您可以简单地使用以下内容:
You can simply use the following:
比
is faster than
您可以使用
strval()
函数进行转换一个数字到一个字符串。从维护的角度来看,很明显您正在尝试做什么,而不是其他一些更深奥的答案。 当然,这取决于您的背景。
You can use the
strval()
function to convert a number to a string.From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context.
有很多方法可以做到这一点。
两个示例:
请参阅有关 类型的 PHP 手册杂耍以获得更多。
There's many ways to do this.
Two examples:
See the PHP Manual on Types Juggling for more.
现在
$foo
是一个字符串。但是,您可能想习惯铸造。 由于强制转换是完成此类操作的正确方法:
另一种方法是将其封装在引号中:
Now
$foo
is a string.But, you may want to get used to casting. As casting is the proper way to accomplish something of that sort:
Another way is to encapsulate in quotes:
使用:
或者它可以是:
或者:
Use:
Or it could be:
Or:
在 PHP 中,有多种方法可以将整数“转换”为字符串。
传统的计算机科学方法是将变量转换为字符串:
您还可以利用 PHP 的隐式类型转换和字符串插值:
最后,与上面类似,任何接受并返回字符串的函数都可以用于转换和整数。 请考虑以下事项:
我不会推荐最终选项,但我在野外看到过依赖此行为的代码,因此我认为我会传递它。
There are a number of ways to "convert" an integer to a string in PHP.
The traditional computer science way would be to cast the variable as a string:
You could also take advantage of PHP's implicit type conversion and string interpolation:
Finally, similar to the above, any function that accepts and returns a string could be used to convert and integer. Consider the following:
I wouldn't recommend the final option, but I've seen code in the wild that relied on this behavior, so thought I'd pass it along.
所有这些答案都很好,但如果值为零,它们都会返回一个空字符串。
请尝试以下操作:
All these answers are great, but they all return you an empty string if the value is zero.
Try the following:
有多种可能的转换方式:
There are many possible conversion ways: