PHP code example of mattsmithdev / faker-small-english
1. Go to this page and download the library: Download mattsmithdev/faker-small-english library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mattsmithdev / faker-small-english example snippets
# When installed via composer
// use the factory to create a Faker\Generator instance
$faker = Mattsmithdev\FakerSmallEnglish\Factory::create();
// generate data by accessing properties
echo $faker->name();
// 'Lucy Cechtelar';
echo $faker->address();
// "426 Jordy Lodge
// Cartwrightshire, SC 88120-6700"
echo $faker->text();
// Dolores sit sint laboriosam dolorem culpa et autem. Beatae nam sunt fugit
// et sit et mollitia sed.
// Fuga deserunt tempora facere magni omnis. Omnis quia temporibus laudantium
// sit minima sint.
for ($i = 0; $i < 10; $i++) {
echo $faker->name(), "\n";
}
// Adaline Reichel
// Dr. Santa Prosacco DVM
// Noemy Vandervort V
// Lexi O'Conner
// Gracie Weber
// Roscoe Johns
// Emmett Lebsack
// Keegan Thiel
// Wellington Koelpin II
// Ms. Karley Kiehn V
// unique() forces providers to return unique values
$values = array();
for ($i = 0; $i < 10; $i++) {
// get a random digit, but always a new one, to avoid duplicates
$values []= $faker->unique()->randomDigit;
}
print_r($values); // [4, 1, 8, 5, 0, 2, 6, 9, 7, 3]
// providers with a limited range will throw an exception when no new unique value can be generated
$values = array();
try {
for ($i = 0; $i < 10; $i++) {
$values []= $faker->unique()->randomDigitNotNull;
}
} catch (\OverflowException $e) {
echo "There are only 9 unique digits not null, Faker can't generate 10 of them!";
}
// you can reset the unique modifier for all providers by passing true as first argument
$faker->unique($reset = true)->randomDigitNotNull; // will not throw OverflowException since unique() was reset
// tip: unique() keeps one array of values per provider
// optional() sometimes bypasses the provider to return a default value instead (which defaults to NULL)
$values = array();
for ($i = 0; $i < 10; $i++) {
// get a random digit, but also null sometimes
$values []= $faker->optional()->randomDigit;
}
print_r($values); // [1, 4, null, 9, 5, null, null, 4, 6, null]
// optional() accepts a weight argument to specify the probability of receiving the default value.
// 0 will always return the default value; 1 will always return the provider. Default weight is 0.5 (50% chance).
$faker->optional($weight = 0.1)->randomDigit; // 90% chance of NULL
$faker->optional($weight = 0.9)->randomDigit; // 10% chance of NULL
// optional() accepts a default argument to specify the default value to return.
// Defaults to NULL.
$faker->optional($weight = 0.5, $default = false)->randomDigit; // 50% chance of FALSE
$faker->optional($weight = 0.9, $default = 'abc')->word; // 10% chance of 'abc'
// valid() only accepts valid values according to the passed validator functions
$values = array();
$evenValidator = function($digit) {
return $digit % 2 === 0;
};
for ($i = 0; $i < 10; $i++) {
$values []= $faker->valid($evenValidator)->randomDigit;
}
print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6]
// just like unique(), valid() throws an overflow exception when it can't generate a valid value
$values = array();
try {
$faker->valid($evenValidator)->randomElement([1, 3, 5, 7, 9]);
} catch (\OverflowException $e) {
echo "Can't pick an even number in that set!";
}
$faker->optional()->passthrough(mt_rand(5, 15));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.