Current Location: Home> Latest Articles> Use array_fill() to set default values ​​for form fields in batches

Use array_fill() to set default values ​​for form fields in batches

M66 2025-06-05

When developing PHP applications, we often need to set default values ​​in batches for form fields. Especially when form submission, we want to be able to automatically fill the default values ​​for those fields when the user does not fill in some fields. array_fill() is a very useful PHP function that can help us achieve this quickly.

This article will introduce how to set default values ​​in batches for multiple form fields using array_fill() and give relevant code examples.

What is the array_fill() function?

array_fill() is a built-in function in PHP that creates an array and sets the same default value for each element in the array. The basic syntax of this function is as follows:

 array_fill(int $start_index, int $num, mixed $value): array
  • $start_index : The starting index of the array.

  • $num : The number of elements in the array.

  • $value : The value to be filled.

By using array_fill() we can easily create an array of specified size and give each element the same default value.

How to set default values ​​for form fields in batches using array_fill() ?

Suppose we have a form with multiple input boxes that are used to collect basic information about the user (such as name, email, phone number, etc.). We can set default values ​​in batches for these form fields using array_fill() . Here is an example:

 <?php
// Define form fields
$form_fields = ['name', 'email', 'phone', 'address'];

// use array_fill() Set default values ​​for each field
$default_values = array_fill(0, count($form_fields), '');

// Populate the default value into the form field
$form_data = array_combine($form_fields, $default_values);

// Output form fields and default values
print_r($form_data);
?>

Code parsing:

  1. Defining form fields : First, we define an array $form_fields containing the form field names, such as name , email , phone , address , etc.

  2. Set the default value using array_fill() : Next, we use array_fill() to create an empty array of values ​​for each field. count($form_fields) calculates the number of fields, we use this number to specify the size of the array and set the default value of each element to an empty string '' .

  3. Apply the default value to the field : The array_combine() function combines the field name and the default value into an associative array to form the final form data.

  4. Output result : Finally, use print_r() to output the form field and its corresponding default value.

Complete code example:

 <?php
// Form field name
$form_fields = ['name', 'email', 'phone', 'address'];

// use array_fill() Set default values ​​for each field
$default_values = array_fill(0, count($form_fields), '');

// Merge field names and default values
$form_data = array_combine($form_fields, $default_values);

// Show form in page
echo '<form action="https://m66.net/submit_form.php" method="post">';
foreach ($form_data as $field => $default_value) {
    echo '<label for="' . $field . '">' . ucfirst($field) . ':</label>';
    echo '<input type="text" name="' . $field . '" value="' . htmlspecialchars($default_value) . '"><br>';
}
echo '<input type="submit" value="submit">';
echo '</form>';
?>

Code parsing:

  1. Form field name : We still use the $form_fields array to define the fields of the form.

  2. Default value setting : Set the default value of each field to an empty string through array_fill() .

  3. Generate HTML forms : Use foreach to loop through the $form_data array and generate an input box <input> for each form field. In the value property, we set the default value of the field.

  4. Form Submission : The target URL of form submission is set to https://m66.net/submit_form.php , indicating that the form data will be submitted to this address.

Summarize

By using the array_fill() function, we can easily set default values ​​in batches for multiple form fields. This method is very convenient, especially when dealing with large forms containing multiple input boxes, which can greatly reduce the redundancy of the code and improve development efficiency.

I hope this article can help you better understand the usage of array_fill() and improve your work efficiency in actual development!