Is there a way to generate a 5 digit zip code/postcode using fzaninotto's Faker?
His provided function to do so, $faker->postcode
, does not offer parameters to limit its size, so it often gives a 9-digit postcode instead of 5, e.g., 10278-4159 instead of 10278. This is problematic for seeding a database, because my sql database has a zipcode column of INT(5).
I know I can resort to just generating a random 5 digit number with Faker, but I wanted to know if there was a native way of generative 5 digit zip codes.
1 Expert Answer There are ten possible digits (0 to 9). This means 100000 possible zip codes if repeatable. 540 * 56 = 30240 zip codes without any repetitions.
ZIP Codes are numbered with the first digit representing a certain group of U.S. states, the second and third digits together representing a region in that group (or perhaps a large city) and the fourth and fifth digits representing a group of delivery addresses within that region.
A postal code (also known locally in various English-speaking countries throughout the world as a postcode, post code, PIN or ZIP Code) is a series of letters or digits or both, sometimes including spaces or punctuation, included in a postal address for the purpose of sorting mail.
Number of ways 4 (digit-zipcode) chosen without repetition from 10 digits (0, 1,2,…,9) is the combination 4 out of 10, i.e 10!/4! x6! = 210 different combinations. In combination, the order of digit does not matter, but since zip code 1204 is different from 4012 or 0241, for each of 210 combinations we have 4!
A workaround I found uses the postcode of the address class which always returns 5 digits.
Instead of $faker->postcode
you can call Address::postcode()
.
Don't forget to include use Faker\Provider\Address;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With