I'am doing a call to a database to retrieve an ID of a specific item. Usually there are the ids like: 1, 2, 14, 23, ...
. I need to get the ID from a DB and output it 4 digit number.
For exemple: My result ID is: 1
. Now i when i have this value, i want it to become 0001
. Or if my result id is 13
, it should became 0013
, etc.
How can i achieve that with php without changing the real ID in database?
You want to zerofill an integer.
You could either do sprintf('%04u', $n)
or str_pad($n, 4, '0', STR_PAD_LEFT)
.
一直如此。
sprintf("%04d", $num)
You want the PHP strpad function:
<?php
$input = 1;
echo str_pad($input, 4, "0", STR_PAD_LEFT); // produces "0001"
?>
如果你想在MySQL查询中做到这一点,就在这里
SELECT LPAD(id, 4, '0') as modified_id FROM table;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.