繁体   English   中英

PHP 8位数问题

[英]PHP 8 digit number question

如何将8位数作为标准数字。 该数字将从数据库获取用户ID。

user_id = 1 // This should should be echo as 00000001
user_id = 11 // This should should be echo as 00000011
user_id = 111 // This should should be echo as 00000111

我该如何编码呢? 请帮助谢谢。

您可以使用带有%08s printf函数作为格式字符串:

printf("%08s",$user_id);

如果要将结果存储回字符串,可以使用sprintf

$user_id = sprintf("%08s",$user_id);

格式说明符%08s表示:

s : Interpret the argument as a string
8 : Print the string left justified within 8 alloted places
0 : Fill the unused places with 0

你可以使用printf

printf("%08d", $user_id);

PHP有sprintf

$user_str = sprintf("%08d", $user_id);
echo $user_str;

你可以用str_pad

   echo str_pad($user_id,8,'0',STR_PAD_LEFT);

$ user_id = str_pad($ user_id,8,“0”,STR_PAD_LEFT);

function leadingZeroes($number, $paddingPlaces = 3) {
    return sprintf('%0' . $paddingPlaces . 'd', $number);
}

来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM