Add 0's before number (PHP)

  • Posted on: 10 January 2017
  • By: jmu

str_pad

$example = 12;
$result = str_pad($example,4,"0",STR_PAD_LEFT);  //0012

 

Hint:Optional argument pad_type can be STR_PAD_RIGHTSTR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.

English