1$arr['John'] = 20;
2$arr['Peter'] = 30;
3
4$foreach = function ($array, $param) {
5 $result = '';
6 foreach ($array as $key => $value) {
7 $search = ["{key}", "{value}"];
8 $replace = [$key, $value];
9 $result .= str_replace($search, $replace, $param);
10 }
11
12 return $result;
13};
14
15echo <<<HTML
16 <ul>
17 {$foreach($arr, '<li>{key} is {value} years old</li>')}
18 <ul>
19HTML;