1Installation#
2Faker requires PHP >= 7.1.
3
4
5composer require fakerphp/faker
6Basic Usage#
7Autoloading#
8Faker supports both PSR-0 as PSR-4 autoloaders.
9
10
11// when installed via composer
12require_once 'vendor/autoload.php';
13You can also load Fakers shipped PSR-0 autoloader
14
15
16// load Faker autoloader
17require_once '/path/to/Faker/src/autoload.php';
18alternatively, you can use any other PSR-4 compliant autoloader
19
20Create fake data#
21Use Faker\Factory::create() to create and initialize a faker generator, which can generate data by calling methods named after the type of data you want.
22
23
24require_once 'vendor/autoload.php';
25
26// use the factory to create a Faker\Generator instance
27$faker = Faker\Factory::create();
28// generate data by calling methods
29echo $faker->name();
30// 'Vince Sporer'
31echo $faker->email();
32// 'walter.sophia@hotmail.com'
33echo $faker->text();
34// 'Numquam ut mollitia at consequuntur inventore dolorem.'
35Each call to $faker->name() yields a different (random) result. This is because Faker uses __call() magic, and forwards Faker\Generator->$method() calls to Faker\Generator->format($method, $attributes).
36
37
38for ($i = 0; $i < 3; $i++) {
39 echo $faker->name() . "\n";
40}
41
42// 'Cyrus Boyle'
43// 'Alena Cummerata'
44// 'Orlo Bergstrom'