在计数低于 10 时显示两位数


show double digit numbers in count below 10?

可能的重复项:
字符串中的零位键盘数字

我使用此计数脚本来显示用户拥有的新消息数。 该脚本显示用户使用 count 拥有的任何新消息数的数字值。

目前,如果数字低于 10,则数字将显示为 1、2、3 等,但我希望它以两位数显示为 01、02、03、04 等。

这可能吗。这是我到目前为止的代码?

<?php
$check_new_messages_set = check_new_messages();
while ($new = mysql_fetch_array($check_new_messages_set)) {
echo "<div class='"msg-notify'">". $new['COUNT(id)'] ."</div>"; 
?>

使用 sprintf() 进行简单的填充:

$padded = sprintf('%02d', $number); // 2 -> 02

另一种选择是str_pad,无论哪种方式,您最终都会得到一个字符串,但如果它只是为了显示,那不是问题