我想将整数转换为字符串,同时保留前导0's。
这可能吗PHP?
$num = 0000003;
echo $num; // output: 3
echo (string)$num; // output: 3
// Wanted output: "0000003"
Run Code Online (Sandbox Code Playgroud)
编辑:
我之前在 SO 中读过最多的问题,但我很好奇是否有可能保留0并在运行时将其转换为字符串,就像上面的代码示例一样echo (string)$num;。
小智 6
你不能。运行时的0000003只是3。
您可以使用str_pad手动添加零,但您必须知道目标字符串长度
str_pad((string)$num, $targetLenght, '0', STR_PAD_LEFT)
Run Code Online (Sandbox Code Playgroud)