在PHP中有一种将整数转换为字符串的方法吗?
我的情况:
echo strval("12"); => 12
echo strval("0"); => "0"
我正在编程工作中...
$a = "12";
$b = "0";
echo $a * 1; => 12
echo $b * 1; => 0
$amount = 2351.25;
$str_amount = "2351.25";
$strCorrectAmount = "$amount";
echo gettype($strCorrectAmount); //string
所以回显将返回字符串。
我尝试了上面的所有方法,但是当我将值嵌入到另一个字符串中时,我得到了“数组转换为字符串”的错误。如果你和我遇到了同样的问题,请尝试使用implode()函数。 例如:
$integer = 0;
$id = implode($integer);
$text = "Your user ID is: ".$id ;
Argument #1 ($pieces) 必须是数组类型,但实际给定的是字符串。
- Your Common Sense$intVal = 5;
$strVal = trim($intVal);
trim()
函数从字符串的两侧删除空格和其他预定义字符,虽然它返回字符串,但这是一种不良实践。最好使用strval()
或number_format()
。 - Marwan Salim$integer = 93;
$stringedInt = $integer.'';
比
更快。$integer = 93;
$stringedInt = $integer."";
$foo = 5;
$foo = "$foo";
这段代码只是用字符串覆盖了整数值,当然,这不是答案,但是什么是“浪费”? - JEX
echo
这个函数中被视为字符串类型(即所谓的“字符串上下文”)。 - hakrestrval()
函数在内部不会改变$variable
变量的值。 - hakre