Add Zero before US Zipcode/Pincode

Add Zero before US Zipcode/Pincode

  • 14 July, 2018
  • Renish Khunt
  • PHP

Many times US zipcode/Pincode starting with zero ( 00501, 00503 ), I am working on a project on that Many users do not enter the full Zipcode, User entering only ( 501, 503 ) without Zero. I am using that Zipcode/Pincode for fetching the User location lat lang using Google Geolocation API. For, Fetching proper location form Google API I should pass valid Zipcode/Pincode with zero. Let’s see the easiest solution for add Zero before US Zipcode/Pincode.

Add ZERO before Zipcode/Pincode

print str_pad('501', 5, 0, STR_PAD_LEFT);  // Output : 00501

Add ZERO after Zipcode/Pincode

print str_pad('501', 5, 0, STR_PAD_RIGHT); // Output : 50100

The easiest way to adding the ZERO before Zipcode/Pincode using “str_pad” function.