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

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

问题描述:

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

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

你可以使用 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.

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