Formatting a number with leading zeros in PHP
Use sprintf
:
sprintf('%08d', 1234567);
Alternatively you can also use str_pad
:
str_pad($value, 8, '0', STR_PAD_LEFT);
What does a leading zero do for a php int?
If you use a leading zero, the number is interpreted by PHP as an octal number. Thus, a 9
is not a valid part of the number and the parser stops there:
0730320 (base 8)
= 7 * 8^5 + 3 * 8^4 + 3 * 8^2 + 2 * 8^1
= 7 * 32768 + 3 * 4096 + 3 * 64 + 2 * 8 (base 10)
= 241872 (base 10)
How is Leading Zero working in php?
When you preceed integers with zero in PHP, in that instance, 029
.
It becomes octal.
So when you echo that, it will convert to its decimal form.
Which results to:
echo 016; //14 (decimal) valid octal
echo 029; // 2 (decimal) - Invalid octal
Actually, its here stated in the manual
Valid octal:
octal : 0[0-7]+
Note: Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.
How to keep leading zeros in PHP integer
You cannot keep leading zeros in integer. You can either keep them in a string or add at output time, using sprintf()
, str_pad()
, etc.
Adding numbers with leading zeros
With str_pad:
For($i=1; $i<=44; $i++){
Echo str_pad($i,5,"0",STR_PAD_LEFT) ."\n";
}
https://3v4l.org/Pe3M2
Str_pad(start number, length of string you want, what type of padding, where to place the padding [left/right])
The leading zero is changing the result in php
Appending 0 to the front of a number in PHP is making it octal.
16 in octal is equivalent to 14 in decimal.
So , 14/2 = 7 is the answer.
PHP prepend leading zero before single digit number, on-the-fly
You can use sprintf: http://php.net/manual/en/function.sprintf.php
<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>
It will only add the zero if it's less than the required number of characters.
Edit: As pointed out by @FelipeAls:
When working with numbers, you should use %d
(rather than %s
), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.
For example:
sprintf("%04s", 10);
returns 0010sprintf("%04s", -10);
returns 0-10
Where as:
sprintf("%04d", 10);
returns 0010sprintf("%04d", -10);
returns -010
Related Topics
PHP Fatal Error: Call to a Member Function Format() on Boolean
What Does Bind_Param Accomplish
MySQL Group by and Fill Empty Rows
Why Preg_Replace Throws Me a "Unknown Modifier" Error
How to Send an HTML Email with an Inline Attached Image with PHP
Detect If Cookies Are Enabled in PHP
How to Sum All Values of a Specific Array Key Recursively
PHP Prepared Statement Problems
Html5 Audio Says "Live Broadcast" in iOS When It's a Static File
How to Detect Fake Users ( Crawlers ) and Curl
How to Efficiently Use Try...Catch Blocks in PHP
How to Display the Users Profile Pic Using the Facebook Graph API
Gmail Like File Upload with Jquery
Get All Records from MySQL Database That Are Within Google Maps .Getbounds
Facebook Sdk V4 for PHP Minimal Example
Trying to Pass Todataurl with Over 524288 Bytes Using Input Type Text