Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jericopulvera's avatar

How do I generate an ID with minimum length of 6?

e.g

1 = 000001

10 = 000010

100 = 000100

1000 = 001000

is it advisable to do this when storing to db as booking id?

0 likes
5 replies
option's avatar

I'm more so wondering why you'd want to if the ID is running Auto Increment anyway?

gator's avatar

A simple str_pad() call should do the trick:

$bookingID = str_pad($id,6,"0",STR_PAD_LEFT);
1 like
Jaytee's avatar

@option Sometimes you don't want to expose a record's ID.

For example (it's a shitty example, but it's valid):

What if you had a table that houses all paying members? Well you don't really want to expose the ID, because then people know how much your income is etc. All they'd need to do is keep changing the number in the URL until an invalid record is returned. Then they know exactly how many customers you have.

Please or to participate in this conversation.