Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP number format 4 digits [duplicate]

For example, I have this format: $s_number = "12"; And for this input, I need 0012 as the output:

Another example:

Input $s_number = "3"; Output: 0003

Every time I need 4 digits I would like this to happen.

like image 749
Martelo2302 Avatar asked Mar 05 '13 21:03

Martelo2302


2 Answers

It won't be a number (but a string), but you can do that using str_pad. In your examples:

$s_number = str_pad( "12", 4, "0", STR_PAD_LEFT );
$s_number = str_pad( "3", 4, "0", STR_PAD_LEFT );
like image 66
Vivienne Avatar answered Nov 09 '22 21:11

Vivienne


Use str_pad() for that:

echo str_pad($number, 4, '0', STR_PAD_LEFT); 
like image 13
hek2mgl Avatar answered Nov 09 '22 23:11

hek2mgl