在 PHP 中将整数转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1035634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting an integer to a string in PHP
提问by kman99
Is there a way to convert an integer to a string in PHP?
有没有办法在PHP中将整数转换为字符串?
回答by Chris Thompson
You can use the strval()function to convert a number to a string.
您可以使用该strval()函数将数字转换为字符串。
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.
从维护的角度来看,您想要做的事情很明显,而不是其他一些更深奥的答案。当然,这取决于您的上下文。
$var = 5;
// Inline variable parsing
echo "I'd like {$var} waffles"; // = "I'd like 5 waffles
// String concatenation
echo "I'd like ".$var." waffles"; // I'd like 5 waffles
// Explicit cast
$items = (string)$var; // $items === "5";
// Function call
$items = strval($var); // $items === "5";
回答by Sanjay Sheth
There's many ways to do this.
有很多方法可以做到这一点。
Two examples:
两个例子:
$str = (string) $int;
$str = "$int";
See the PHP Manual on Types Jugglingfor more.
有关更多信息,请参阅关于类型杂耍的 PHP 手册。
回答by Sev
$foo = 5;
$foo = $foo . "";
Now $foois a string.
现在$foo是一个字符串。
But, you may want to get used to casting. As casting is the proper way to accomplish something of that sort:
但是,您可能想要习惯投射。因为铸造是完成这类事情的正确方法:
$foo = 5;
$foo = (string)$foo;
Another way is to encapsulate in quotes:
另一种方法是用引号封装:
$foo = 5;
$foo = "$foo"
回答by Alan Storm
There are a number of ways to "convert" an integer to a string in PHP.
在 PHP 中有多种方法可以将整数“转换”为字符串。
The traditional computer science way would be to cast the variable as a string:
传统的计算机科学方法是将变量转换为字符串:
$int = 5;
$int_as_string = (string) $int;
echo $int . ' is a '. gettype($int) . "\n";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
You could also take advantage of PHP's implicit type conversion and string interpolation:
您还可以利用 PHP 的隐式类型转换和字符串插值:
$int = 5;
echo $int . ' is a '. gettype($int) . "\n";
$int_as_string = "$int";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
$string_int = $int.'';
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
Finally, similar to the above, any function that accepts and returns a string could be used to convert and integer. Consider the following:
最后,与上面类似,任何接受并返回字符串的函数都可以用于转换和整数。考虑以下:
$int = 5;
echo $int . ' is a '. gettype($int) . "\n";
$int_as_string = trim($int);
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
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.
我不推荐最后一个选项,但我在野外看到过依赖这种行为的代码,所以我想我会传递它。
回答by Microprocessor Cat
All these answers are great, but they all return you an empty string if the value is zero.
所有这些答案都很棒,但如果值为零,它们都会返回一个空字符串。
Try the following:
请尝试以下操作:
$v = 0;
$s = (string)$v ? (string)$v : "0";
回答by gewel
Use:
用:
$intValue = 1;
$string = sprintf('%d', $intValue);
Or it could be:
或者它可能是:
$string = (string)$intValue;
Or:
或者:
settype(&$intValue, 'string');
回答by Usersbs
There are many possible conversion ways:
有多种可能的转换方式:
$input => 123
sprintf('%d',$input) => 123
(string)$input => 123
strval($input) => 123
settype($input, "string") => 123
回答by Andrew Dunkman
You can either use the period operator and concatenate a string to it (and it will be type casted to a string):
您可以使用句点运算符并将字符串连接到它(它将被强制转换为字符串):
$integer = 93;
$stringedInt = $integer . "";
Or, more correctly, you can just type cast the integer to a string:
或者,更准确地说,您可以将整数类型转换为字符串:
$integer = 93;
$stringedInt = (string) $integer;
回答by troelskn
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.
正如这里的答案很好地展示的那样,是的,有几种方法。但是,在 PHP 中,您实际上很少需要这样做。编写 PHP 的“教条方式”是依赖于语言的松散类型系统,它将根据需要透明地强制类型。对于整数值,这通常没有问题。但是,您应该非常小心浮点值。
回答by merkuro
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...
我会说这取决于上下文。可以使用 strval() 或转换运算符(字符串)。但是,在大多数情况下,PHP 会决定什么对您有好处,例如,如果您将它与 echo 或 printf 一起使用...
One small note: die() needs a string and won't show any int :)
一个小提示:die() 需要一个字符串并且不会显示任何 int :)

