0

你好,

我有这个代码很好用

function show_aval($place) {
   echo $place;
}

ob_start();
show_aval(london);
$show_aval = ob_get_contents();
ob_clean();

ob_start();
show_aval(york);
$show_aval2 = ob_get_contents();
ob_clean();

ob_start();
show_aval(liverpool);
$show_aval3 = ob_get_contents();
ob_clean();

但我想像这样简化这段代码:

$avalrooms = [];
$cities = ["london", "york", "liverpool"];
foreach ($cities as $city) {
    ob_start();
    show_aval($city);
    $avalrooms[$city] = ob_get_contents();
    ob_clean;
}

但是,这似乎无效ob_get_contents,因为该功能正在按show_aval($city);所述立即执行。

为什么会这样?

谢谢你。

4

0 回答 0