php pad leading zeros code example

Example 1: php 0 padding left

<?php
$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
echo str_pad($input,  6, "___");               // produces "Alien_"
echo str_pad($input,  3, "*");                 // produces "Alien"
?>

Example 2: prepend 0 to number php

str_pad($month, 2, '0', STR_PAD_LEFT);

Example 3: php format int to 9 digits with preceding zeroes

str_pad($input, 9, "0", STR_PAD_LEFT);

Example 4: php pad leading zeros

str_pad($value, 8, '0', STR_PAD_LEFT);

Tags:

Misc Example