在 PHP 中将整数转换为字符串

发布于 2024-07-25 11:57:13 字数 27 浏览 5 评论 0原文

PHP 中有没有办法将整数转换为字符串?

Is there a way to convert an integer to a string in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

孤芳又自赏 2024-08-01 11:57:14

您可以使用句点运算符并将一个字符串连接到它(它将被类型转换为字符串):

$integer = 93;
$stringedInt = $integer . "";

或者,更正确地说,您可以将整数类型转换为字符串:

$integer = 93;
$stringedInt = (string) $integer;

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;
橘和柠 2024-08-01 11:57:14

正如这里的答案很好地表明的那样,是的,有多种方法。 然而,在 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.

短叹 2024-08-01 11:57:14

我想说这取决于上下文。 可以使用 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 :)

煞人兵器 2024-08-01 11:57:14
$amount = 2351.25;
$str_amount = "2351.25";

$strCorrectAmount = "$amount";
echo gettype($strCorrectAmount);    //string

所以回显将返回字符串

$amount = 2351.25;
$str_amount = "2351.25";

$strCorrectAmount = "$amount";
echo gettype($strCorrectAmount);    //string

So the echo will be return string.

绮烟 2024-08-01 11:57:14

我的情况:

echo strval("12"); => 12
echo strval("0"); => "0"

我正在工作...

$a = "12";
$b = "0";
echo $a * 1; => 12
echo $b * 1; => 0

My situation :

echo strval("12"); => 12
echo strval("0"); => "0"

I'm working ...

$a = "12";
$b = "0";
echo $a * 1; => 12
echo $b * 1; => 0

坐在坟头思考人生 2024-08-01 11:57:14

我尝试了上面的所有方法,但当我将值嵌入到另一个字符串中时,出现“数组到字符串转换”错误。 如果您遇到与我同样的问题,请尝试 implode() 函数。
例子:

$integer = 0;    
$id = implode($integer);    
$text = "Your user ID is: ".$id ;

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:

$integer = 0;    
$id = implode($integer);    
$text = "Your user ID is: ".$id ;
温柔嚣张 2024-08-01 11:57:14

您可以简单地使用以下内容:

$intVal = 5;
$strVal = trim($intVal);

You can simply use the following:

$intVal = 5;
$strVal = trim($intVal);
天煞孤星 2024-08-01 11:57:14
$integer = 93;
$stringedInt = $integer.'';

$integer = 93;
$stringedInt = $integer."";
$integer = 93;
$stringedInt = $integer.'';

is faster than

$integer = 93;
$stringedInt = $integer."";
丿*梦醉红颜 2024-08-01 11:57:13

您可以使用 strval() 函数进行转换一个数字到一个字符串。

从维护的角度来看,很明显您正在尝试做什么,而不是其他一些更深奥的答案。 当然,这取决于您的背景。

$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

// The two examples above have the same end value...
// ... And so do the two below

// Explicit cast 
$items = (string)$var; // $items === "5";

// Function call
$items = strval($var); // $items === "5";

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.

$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

// The two examples above have the same end value...
// ... And so do the two below

// Explicit cast 
$items = (string)$var; // $items === "5";

// Function call
$items = strval($var); // $items === "5";
猥︴琐丶欲为 2024-08-01 11:57:13

有很多方法可以做到这一点。

两个示例:

 $str = (string) $int;
 $str = "$int";     

请参阅有关 类型的 PHP 手册杂耍以获得更多。

There's many ways to do this.

Two examples:

 $str = (string) $int;
 $str = "$int";     

See the PHP Manual on Types Juggling for more.

森罗 2024-08-01 11:57:13
$foo = 5;

$foo = $foo . "";

现在 $foo 是一个字符串。

但是,您可能想习惯铸造。 由于强制转换是完成此类操作的正确方法:

$foo = 5;
$foo = (string)$foo;

另一种方法是将其封装在引号中:

$foo = 5;
$foo = "$foo"
$foo = 5;

$foo = $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:

$foo = 5;
$foo = (string)$foo;

Another way is to encapsulate in quotes:

$foo = 5;
$foo = "$foo"
强者自强 2024-08-01 11:57:13

使用:

$intValue = 1;
$string = sprintf('%d', $intValue);

或者它可以是:

$string = (string)$intValue;

或者:

settype($intValue, 'string');

Use:

$intValue = 1;
$string = sprintf('%d', $intValue);

Or it could be:

$string = (string)$intValue;

Or:

settype($intValue, 'string');
旧城烟雨 2024-08-01 11:57:13

在 PHP 中,有多种方法可以将整数“转换”为字符串。

传统的计算机科学方法是将变量转换为字符串:

$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";

您还可以利用 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";

最后,与上面类似,任何接受并返回字符串的函数都可以用于转换和整数。 请考虑以下事项:

$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";

我不会推荐最终选项,但我在野外看到过依赖此行为的代码,因此我认为我会传递它。

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:

$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:

$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.

べ映画 2024-08-01 11:57:13

警告:以下答案基于错误的前提。 将 0 数字转换为字符串始终返回字符串“0”,从而使提供的代码变得多余。

所有这些答案都很好,但如果值为零,它们都会返回一个空字符串。

请尝试以下操作:

    $v = 0;

    $s = (string)$v ? (string)$v : "0";

Warning: the below answer is based on the wrong premise. Casting 0 number to string always returns string "0", making the code provided redundant.

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";
愛上了 2024-08-01 11:57:13

有多种可能的转换方式:

$input => 123
sprintf('%d',$input) => 123
(string)$input => 123
strval($input) => 123
settype($input, "string") => 123

There are many possible conversion ways:

$input => 123
sprintf('%d',$input) => 123
(string)$input => 123
strval($input) => 123
settype($input, "string") => 123
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文