Backend Development 6 min read

How to Define, Pass, Iterate, and Return Arrays in PHP Functions

This article explains how to define arrays using array(), pass them as function parameters, iterate over them with for and foreach loops, and return modified arrays in PHP functions, providing complete code examples for each step.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Define, Pass, Iterate, and Return Arrays in PHP Functions

In PHP programming, arrays are a common and important data type that can store multiple values as key‑value pairs and are useful in many scenarios. When writing custom PHP functions, you often need to work with arrays.

1. Define an array

In PHP you can use the array() function to define an array. For example, to create an array containing the numbers 1, 2, 3:

$my_array = array(1, 2, 3);

You can define an array inside a function in the same way:

function my_function() {
    $my_array = array(1, 2, 3);
}

In this example we defined a function named my_function and created an array called $my_array inside it.

2. Pass an array as a parameter

When a function needs to operate on an array, you pass the array to the function. In PHP this is done as follows:

function my_function($my_array) {
    // operate on $my_array
}

Here the function my_function receives the array $my_array as an argument.

3. Iterate over an array

To work with each element of an array you can iterate over it. PHP provides two common ways:

Using a for loop

function my_function($my_array) {
    for ($i = 0; $i < count($my_array); $i++) {
        echo $my_array[$i] . "
";
    }
}

This example uses a for loop to print each element of $my_array .

Using a foreach loop

function my_function($my_array) {
    foreach ($my_array as $value) {
        echo $value . "
";
    }
}

This example uses foreach to print each element of $my_array .

4. Return an array

After processing an array inside a function you may want to return the modified array. Use the return statement:

return $my_array;

In the example the modified $my_array is returned to the caller.

5. Full example

The following complete example demonstrates defining, passing, iterating, and returning an array within a PHP function:

function my_function($my_array) {
    for ($i = 0; $i < count($my_array); $i++) {
        $my_array[$i] = $my_array[$i] * $my_array[$i];
    }
    return $my_array;
}

$my_input_array = array(1, 2, 3);
$my_output_array = my_function($my_input_array);

foreach ($my_output_array as $value) {
    echo $value . "
";
}

In this example the function my_function squares each element of the input array and returns the new array, which is then printed in the main script.

Summary

Defining, passing, iterating, and returning arrays in PHP functions are common operations. Choose the appropriate loop (for or foreach) based on your needs, and ensure proper handling of arrays to write efficient and reliable PHP applications.

Backend DevelopmentPHPFunctionsarraysProgramming Tutorial
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.