Php string pad left 0

string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])

Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example:

<?php
    $string = "Goodbye, Perl!";
    $newstring = str_pad($string, 10);
?>

That code would leave " Goodbye, Perl! " in $newstring, which is the same string from $string except with five spaces on either side, equalling the 10 we passed in as parameter two.

Str_pad() has an optional third parameter that lets you set the padding character to use, so:

<?php
    $string = "Goodbye, Perl!";>
    $newstring = str_pad($string, 10, 'a');
?>

That would put "aaaaaGoodbye, Perl!aaaaa" into $newstring.

We can extend the function even more by using it is optional fourth parameter, which allows us to specify which side we want the padding added to. The fourth parameter is specified as a constant, and you either use STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:

<?php
    $string = "Goodbye, Perl!";
    $a = str_pad($string, 10, '-', STR_PAD_LEFT);
    $b = str_pad($string, 10, '-', STR_PAD_RIGHT);
    $c = str_pad($string, 10, '-', STR_PAD_BOTH);
?>

That code will set $a to be "----------Goodbye, Perl!", $b to be "Goodbye, Perl!----------", and $c to be "-----Goodbye, Perl!-----", as expected.

Note that HTML only allows a maximum of one space at any time. If you want to pad more, you will need to use "&nbsp;", the HTML code for non-breaking space.

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Complex string printing >>

Previous chapter: Comparing strings

Jump to:

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.

Pad a string or zero fill a number using PHP.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

$number = 5;
echo sprintf('%02s', $number); // 05
echo sprintf('%03s', $number); // 005
echo sprintf('%02s', 453); // 453
// sprintf is faster than str_pad

  1. HowTo
  2. PHP Howtos
  3. Properly Format a Number With Leading Zeros in PHP

Created: March-04, 2020 | Updated: April-13, 2021

  1. Use a String to Substitute the Number
  2. substr() to Add Leading Zeros in PHP
  3. printf()/sprintf() to Add Leading Zeros in PHP
  4. str_pad() to Add Leading Zeros in PHP

In PHP, numbers or integers with leading zeros can have different values with unexpected results.

Example:

$number = 0987654321; //this is an octal number
$number = 0x987654321 // this is a hexadecimal number
$number = 0b0987654321 // this is a binary number

To make sure that the number will not lose its natural meaning, there are several ways to try like using a string instead of a number or by using different functions such as substr, printf() / sprintf() and str_pad.

Use a String to Substitute the Number

The easiest approach; just simply to use string as a substitute for the number.

$number = "0987654321";

When To Use:

  1. There’s no required length of the output.
  2. There’s no exception to the number that it always need a leading zero.

substr() to Add Leading Zeros in PHP

This method clips the numbers from the left when the string length exceeded.

If the start is negative, the returned string will start from the start'th character from the end of the string.

Example:

$number = 98765;
$length = 10;
$string = substr(str_repeat(0, $length).$number, - $length);

//output: 0000098765

When To Use:

  1. When there’s a fixed length of the output string.
  2. Adding zeros when the string is less than the length.

printf()/sprintf() to Add Leading Zeros in PHP

To pad an output for a fixed length, when the input is less than the length, and return the string when input is greater.

Example:

$length = 10;
$char = 0;
$type = 'd';
$format = "%{$char}{$length}{$type}"; // or "$010d";

//print and echo
printf($format, 987654321);

//store to a variable
$newFormat = sprintf($format, 987654321);

// output: 0987654321

In the example, the fixed length is set to 10 and the input length is 9, so it adds one zero in the left if using printf()/sprintf.

sprintf() Parameter Values

ParameterDescription
format (Required) The string and how to format the variables.
Possible format values:
%% - Percent sign
%b - Binary
%c - Character referenced to ASCII
%d - decimal number (negative or positive)
%e - Lowercase scientific notation
%E - Uppercase scientific notation
%u - Unsigned decimal number
%f - Float number (local settings aware)
%F - Float number (not local settings aware)
%g - shorter version of %e and %f
%G - shorter version of %E and %F
%o - Octal
%s - String
%x - Hexadecimal (lowercase)
%X - Hexadecimal (uppercase)
arg1 (Required) To be inserted at the first % sign
arg2 (Optional) To be inserted at the second % sign
argg++ (Optional) To be inserted at the third, fourth, etc. % sign

Note:

  • If the input string length is greater than or equal to the pad length, it will only return the string - no characters will be omitted.
  • Padding is only added the length of the input is less than the padding length.

str_pad() to Add Leading Zeros in PHP

This method will pad a string to a new length of specified characters.

Example:

$length = 7;
$string = "12345";
echo str_pad($string,$length,"0", STR_PAD_LEFT);
//output: 0012345

The example above will add the zero to the specified string until it matches the specified length (which is 7 in this case).

  • str_pad() Parameters List
ParameterDescription
string The string to pad
length Specifies the new length of the string. Note: If the value is less than the length of the string, nothing will happen.
pad_string Specifies the string to use for padding. The default value is whitespace.
pad_type Specifies where to pad the string.
Accepted Values:
STR_PAD_BOTH - This will pad both sides of the string.
STR_PAD_LEFT - Pad the left side of the string
STR_PAD_RIGHT - Pad to the right side of the string

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - PHP Function

  • Create a PHP Function With Multiple Returns
  • Use ODBC Functions in PHP
  • Use the sleep() Function in PHP
  • Php string pad left 0

    What is str_ pad in php?

    The str_pad() function is a built-in function in PHP and is used to pad a string to a given length. We can pad the input string by any other string up to a specified length. If we do not pass the other string to the str_pad() function then the input string will be padded by spaces.

    Which of the following function pads one string with another?

    MySQL LPAD() function MySQL LPAD() left pads a string with another string. The actual string, a number indicating the length of the padding in characters (optional) and the string to be used for left padding - all are passed as arguments.

    How do you repeat a string to a specific number of time in PHP?

    The str_repeat() function repeats a string a specified number of times.

    Which of the following function pads one string with another PHP?

    The str_pad() function pads a string to a new length.