954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php number format

A simple question.
I have integer variable $a=2, how can I display it in string format: "SN0002" with 4 '0' inserted? thanks.

michael123
Junior Poster in Training
94 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

You may try the follwoing. I used the similar to form my invoice number:[php] $order_num = 'SN'.substr('00000'.$a,-5,5); // e.g. SN00002
[/php]

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

Thanks for the reply.
I also found another way to do that:
$order_num ="SN".str_pad($a,5,0,STR_PAD_LEFT);

michael123
Junior Poster in Training
94 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Another alternative is the sprintf function:

$order_num = "SN".sprintf("%04d", $a);
Lafinboy
Junior Poster
172 posts since Jul 2004
Reputation Points: 16
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You