Creating Dummy Data in PHP With Faker – Dummy data is fake data created with the aim of being sample data or sample data.
Dummy data is usually used for experimental data on applications. when a developer or programmer is making an application or information system, it must sometimes require some or even a lot of sample data as data to be processed by the system.
Of course, it would be very difficult for the programmer if he had to input data one by one, only for testing data.
Therefore, having Faker would be of great help. because we no longer need to think about data names, addresses, telephone numbers and others.
Because all will be created automatically and quickly by Faker.
Creating Dummy Data in PHP With Faker
Faker is a PHP library that is capable ofgenerate fake data for us. so we don’t have to bother anymore to input fake data. just use faker, then as much data as you need will be made by Faker.
Faker runs on PHP version 5.3.3 and above.
After knowing the meaning of faker and use of fakerlet’s go straight to how to install and use library Faker.
Download Faker
To install faker, make sure friends have installed it composer.
If not, please read the previous tutorial about understanding and how to install composer.
I assume you have installed composer.
Then open CMD or terminal.
Go to your project directory. in this tutorial example, my project name is “example_faker” which is in the htdocs folder.
Then type the following composer command in CMD/terminal.
composer require fzaninotto/faker
|
composer require fzaninotto/faker |
Wait until the faker download process is complete.
Look at the following picture.
After the faker download process is complete, if you install faker using composer as above, the faker library will be placed in a folder vendor in the project friends.
The method above is how to download faker using composer, friends can also download faker manually at https://github.com/fzaninotto/Faker.
How to Install Faker
Provide a PHP file. here i will apply faker on index.php file. please customized.
index.php
|
// If download faker with composer require_once ‘vendor/autoload.php’; ?> |
The syntax example above is for linking PHP files with the Faker library.
But if you download it manually, please adjust it, the point is you have to connect it to the file autoload.php.
That’s how to install it, then we’ll try how to use it.
How to Use Faker
For how to use faker, consider the following example.
index.php
name;
echo “
“;
echo $faker->address;
?>
|
// If download faker with composer require_once ‘vendor/autoload.php’; // initialize faker $faker = Faker\Factory::create(‘id_ID’); // generate data name, address echo $faker->name; echo “ echo $faker->address; ?> |
The syntax example above is an example of how to use the faker library. it’s quite easy.
The following syntax is intended for installation as discussed earlier.
require_once ‘vendor/autoload.php’;
|
require_once ‘vendor/autoload.php’; |
Then initialize faker to $faker variable.
// initialize faker $faker = Faker\Factory::create(‘id_ID’);
|
// initialize faker $faker = Faker\Factory::create(‘id_ID’); |
The id_ID above is an option so that the data generated is Indonesian data, for example Indonesian names, addresses, Indonesian telephone numbers and others. Basically the data is in Indonesian format.
If left blank it will display data in English format by default.
Furthermore, the following syntax aims to create name data.
The following syntax is to create address data.
Try running friends. then the result will be as follows.
Faker generates name and address data for Indonesians.
Easy enough isn’t it.
Creating Lots of Dummy Data With Faker
To create multiple dummy data at once, we can use the for loop.
Because basically, faker will create new data every time it loads.
index.php
name;
echo “
“;
echo $faker->address;
echo “
“;
}
?>
|
// If download faker with composer require_once ‘vendor/autoload.php’; // initialize faker $faker = Faker\Factory::create(‘id_ID’); for($a=0; $a<10; $a++){ // generate data name, address echo $faker->name; echo “ echo $faker->address; echo “ “; } ?> |
And the result will be as follows.
As seen in the image above, faker creates 10 data. according to the command we made in the for loop.
Also read: How to input faker data into a database with PHP and MySQLi
Data Faker
There is a lot of data that we can create with faker. including email data, phone numbers, emojis and more.
In full, friends can also read about how to use faker on https://github.com/fzaninotto/Faker
Also Read: How to Create CRUD With PHP and MySQLi
Thank you, hopefully useful.
Incoming search terms:
- gundla mala / php?isthere=
- Generate data dummy
- id_lang= filHA php?mail access yahoo php4?fid=
- php? pagina = paarlmtb
- sub_category= php? / lanG