menu

arrow_back The output of the array to the screen?

by
0 votes
Good day! Help to understand with a simple purpose in writing the following code displays only the first value of array: 'Apple'. Why not show the remaining values, what's wrong?
<?php

class Summer
{
private $arr;


public function arr()
{
$arr = ['Apple', 'Orange', 'Gramm', 'Metr'];

foreach ($arr as $key) {
return $key . '<br>';
}
}
}

$name = new Summer();

echo $name->arr();

1 Answer

by
 
Best answer
0 votes
Because return returns a value and exits the function.

You can make a loop from a function or build array in a string and return it.

For example

public function arr()
{
$arr = ['Apple', 'Orange', 'Gramm', 'Metr'];

return implode('<br>', $arr);
}