Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php fzaninotto Faker - How to Generate Only 5 Digit Zip Code?

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.

like image 550
Koolstr Avatar asked Dec 13 '15 23:12

Koolstr


People also ask

How many 5 digit zip codes are possible if digits Cannot be repeated?

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.

How are ZIP codes generated?

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.

Can a ZIP Code contain alphabets?

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.

Is ZIP Code permutation or combination?

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!


1 Answers

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;.

like image 111
mshaps Avatar answered Oct 08 '22 06:10

mshaps