How To Generate Fake Data in PHP Using Faker Library

Share this Article
Reading Time: 6 minutes
278 Views

While working in any application, sometimes we need lots of dummy data to test the workflow of entire application. Inside this article we will see how to generate faker data in PHP using faker library.

Faker library needs to be install by using composer. Once install, there are several properties, methods available to generate various type of fake data like Name, Email, Phone number etc.

This article is super easy to understand and implement in php programming. To use Faker library, your system must have composer installed. If don’t, please follow this article to install in Linux Operating System. How To Install Composer in Ubuntu 18.04 ?

Learn More –

To do for this article

  • Create a PHP application
  • Install Faker Library
  • Use Faker methods for data
  • Execute Application

Let’s get started.


Create an Application

Create a folder with name php-faker. Inside this folder create these files –

  • composer.json
  • index.php

We need composer.json file because we will install faker library. Write this curly pairs in composer.json file.

{}

If you don’t write this, probably you will get this error on terminal when we install composer package.


Install Faker Library

Open project in terminal and run this command.

$ composer require fzaninotto/faker

After installation of this package, automatically composer.json file get updated.

{
    "require": {
        "fzaninotto/faker": "^1.9"
    }
}

Also you will see inside your application setup, you have now a vendor folder. This is package installed folder, don’t do anything with that.


Load Faker Library to Application

Open index.php file and use require_once php function to include faker library.

<?php

require_once 'vendor/autoload.php';

// use the factory to create a Faker\Generator instance 
$faker = Faker\Factory::create();

...

Use Faker\Factory::create() to create and initialize a faker generator.


Faker Library Methods

There are several methods and properties available to create dummy data for various fields. We can generate faker data for –

  • Name
  • Email
  • Phone number
  • Image
  • Random Digits

… etc.

For complete list, you can go with this link Faker Library.

<?php
  
require_once 'vendor/autoload.php';
  
// use the factory to create a Faker\Generator instance
$faker = Faker\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.

Formatters

Each of the generator properties (like nameaddress, ...) are called “formatters”. A faker generator has many of them, packaged in “providers”.


Generate 100 Test Users

We will see how can we use Faker library to generate 100 test users with data name, email and phone number.

Open index.php

<?php
  
require_once 'vendor/autoload.php';
  
// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();

for ($i = 0; $i < 100; $i++) {
  echo $faker->name, " - " . $faker->unique()->email . " - " . $faker->phoneNumber . "<br/>";
}

Open browser and run application –

We hope this article helped you to learn How To Generate Fake Data in PHP Using Faker Library in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Read More Articles

Leave a Comment