return an array in php

To return an array in PHP, you can simply add the array as the return value in a function. Here's an example:

main.php
function getArray() {
  $array = [1, 2, 3, 4, 5];
  return $array;
}

$result = getArray();
print_r($result);
110 chars
8 lines

In the above example, the getArray() function returns an array [1, 2, 3, 4, 5]. The returned array is then assigned to the $result variable, and you can use print_r() function to display the contents of the array.

Output:

main.php
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
75 chars
9 lines

Note: It's important to use print_r() or var_dump() to display the array values because echo or print will not show the array structure in a human-readable format.

related categories

gistlibby LogSnag