Greeting, need help to solve this problem.
$x = 5;
$y = 19;
$z = 100;
This number must be in 4 digit only. Example if return 19 it must be 0019. How to added '0' in the beginning of variable.
output:
$x = 0005;
$y = 0019;
$z = 0100;
Greeting, need help to solve this problem.
$x = 5;
$y = 19;
$z = 100;
This number must be in 4 digit only. Example if return 19 it must be 0019. How to added '0' in the beginning of variable.
output:
$x = 0005;
$y = 0019;
$z = 0100;
Use sprintf :
sprintf('%04d', 15);
Alternatively you can also use str_pad:
str_pad($value, 4, '0', STR_PAD_LEFT);
sprintf. I edited it to put it in code blocks easier to read...str_pad works good for a line separator in CLI eg. echo str_pad('', 64, '=') prints a line of 64 ========Use str_pad()
$x = 5;
$y = 19;
$z = 100;
echo str_pad($x, 4, '0', STR_PAD_LEFT)."<br>";
echo str_pad($y, 4, '0', STR_PAD_LEFT)."<br>";
echo str_pad($z, 4, '0', STR_PAD_LEFT);
Output
0005
0019
0100
You guys took all the good answers
$x = 23;
while(strlen($x)<4)$x='0'.$x;
echo $x;
Outputs
0023
Sorry this is all that was left.
$x = 5;
$y = 19;
$z = 100;
$x= "000".$x;
$y= "000".$y;
$z= "000".$z;`
This form is simple or
<?php
$number=12443;
$format = number_format($number, 2, '.', '');
echo $format;
?>