$number = 51; $number = sprintf('%04d',$number);//4 number total no print $number; // outputs 0051 // $number = 8051; $number = sprintf('%04d',$number); print $number; // outputs 8051