關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • PHP 內將數值自動補零

    Posted on December 7th, 2007 Sam Tang No comments

    如果在 PHP 想將數值自動補零,例如以 00, 01, 02 …… 等方式顯示,可以用以下方法:

    $var = 1;
    echo sprintf("%02d", $var);

    這樣就會印出 01,其中 2 是顯示的位數,如果想顯示 001,可以改成 sprintf("%03d", $var);


    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word