![](/img/trans.png)
[英]PHP one digit after decimal point for real numbers and no digits for integer values
[英]Make 4 digit integer from one, two, or three digits integer in PHP
我正在調用數據庫來檢索特定項的ID。 通常有ids,如:1,2,14,23 1, 2, 14, 23, ...
我需要從DB獲取ID並輸出4位數字。
例如:我的結果ID是: 1
。 現在,當我有這個值時,我希望它變成0001
。 或者如果我的結果id是13
,它應該變成0013
等。
如何在不更改數據庫中的真實ID的情況下使用php實現這一目標?
你想要整數填充一個整數。
您可以執行sprintf('%04u', $n)
或str_pad($n, 4, '0', STR_PAD_LEFT)
。
一直如此。
sprintf("%04d", $num)
你想要PHP strpad功能:
<?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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.