9951 explained code solutions for 126 technologies


php-fakerHow do I format a phone number using PHP Faker?


To format a phone number using PHP Faker, you can use the $faker->phoneNumber method. This method will return a random phone number in the format of (###) ###-####.

<?php

$faker = Faker\Factory::create();

$phoneNumber = $faker->phoneNumber;

echo $phoneNumber;

// Output: (819) 675-4337
?>

The code consists of three parts:

  1. $faker = Faker\Factory::create(); - This line instantiates a Faker object.
  2. $phoneNumber = $faker->phoneNumber; - This line assigns a random phone number to the $phoneNumber variable.
  3. echo $phoneNumber; - This line prints the phone number to the screen.

For more information on formatting phone numbers using PHP Faker, please refer to the documentation.

Edit this code on GitHub