模数(%)运算符返回单个零。这是正确的但我想在使用模数(%)运算符后为2。我不想使用 if 条件。
$k = 60;
$result = $k%60;
echo "Result = ".$result;
输出
结果= 0
预期产量
结果= 00
答案 0 :(得分:1)
试一试:
$k = 60;
$result = $k%60;
echo str_pad($result, 2, '0', STR_PAD_LEFT);
或测试此方法:
$result=0
$pad_length = 2;
$pad_char = 0;
$str_type = 'd'; // treats input as integer, and outputs as a (signed)
decimal number
$format = "%{$pad_char}{$pad_length}{$str_type}";
// output and echo
printf($format, $result);