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

Recommended Answers

All 3 Replies

You may try the follwoing. I used the similar to form my invoice number:

$order_num = 'SN'.substr('00000'.$a,-5,5);	// e.g. SN00002

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

Another alternative is the sprintf function:

$order_num = "SN".sprintf("%04d", $a);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.