Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Generate Fake Data In PHP With Faker


Share On     Share On WhatsApp     Share On LinkedIn


Many times for the sake of testing you might have dumped manually form fields fake data to the database. Lot of times while demoing the applications to your client you may have seen this fake junk data from your database which may not make any sense to your clients, even they might not understand lot of things for this purpose.


In this article, you will learn how to generate the fake data with PHP Faker from this package fzaninotto/Faker.


Prerequisites


I hope you have composer installed in your system. Composer is a PHP dependency manager. To install composer you can use the following command


Linux/Ubunut


sudo apt-get update
sudo apt-get install composer



Step 1 - Install PHP Faker Package With Composer


I am creating new project test inside htdocs folder, you might have to create inside /var/www/html or any other working directory for your PHP.


First and the foremost thing is PHP Faker library you can install it with the help of the following command. Go to your htdocs/test project and run the following


composer require fzaninotto/faker


After installation you will have the following project folder structure. After using the above command you will have


Project Structure After Composer Package Installation

Project Structure After Composer Package Installation



Step 2 - Usage In You Projects


For the sake of demonstration I will use fresh plain PHP project so that you will know how to implement in any other PHP frameworks too.


As shown in the above image I will write everything in index.php file.


<?php
require_once './vendor/autoload.php';


Don't forget to add autoload.php file which is inside vendor/autoload.php file.



Step 3 - Generate Fake Name (Default en_US)


Now you can generate the fake data in the following way I have included comments to make sure you understand those


<?php
/** Include autoload.php for autoloading the classes used in Faker */
require_once './vendor/autoload.php';

/** Faker Factory Object */
$faker = Faker\Factory::create();

/** By default it uses en_US names, I will show you how to change this */

/** Full Name Generation */
echo $faker->name."<br>";

/** First Name & Last Name Generation */
echo $faker->firstNameFemale.' '.$faker->lastName."<br>";

/** Female Title like Mrs. Miss. */
/** You can also use $fakerTitleFemale */
echo $faker->title('female')."<br>"; 

/** Male Title like Mr. Mrs. */
/** You can also use $fakerTitleMale */
echo $faker->title('male')."<br>";

/** Jr., II, DDS Make sure this exists in Faker Factory Provider, For Indian locale it wont work */
echo $faker->suffix."<br>";




Step 4- Setting Locale ie Different Country Data (en_IN)


As we saw we know we can see that we are able to see the above details shows some English names, we do have an option to set the different country locale. List of different country options is available in the following


Fzaninotto-Faker


To set the locale use the following code. Here I am using en_IN as the default country data to populate


<?php
/** Include autoload.php for autoloading the classes used in Faker */
require_once './vendor/autoload.php';

/** Faker Factory Object */
$faker = Faker\Factory::create('en_IN');

/** Full Name Generation */
echo $faker->name."<br>";

/** Rest of the above code remains same other than suffix, as suffix wont exist in india */



Step 5 - Multiple Records Generation


As you saw above that you can generate names, if you want to generate for example 50 names then you can use the loop like the following


<?php
/** Include autoload.php for autoloading the classes used in Faker */
require_once './vendor/autoload.php';

/** Faker Factory Object */
$faker = Faker\Factory::create('en_IN');

for($i = 0; $i <= 50; $i++){
  echo $faker->name."<br>";
  /** You can use DB Connection To Store Data */
}



Step 6 - Other Fields


You can generate list of other data like LoremIpsum, Address, Phone, Company, Text, DateTime, Internet, User Agent, Payment and many more.


For more details on the same & how to use those you can use the following GITHUB link for the repo Faker Library


Conclusion


You learnt how to generate the fake data for faster data adding to your database. You might also be interested to learn few more topics I have written




Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing