嗨,
我有这段代码可以正常工作
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);
之后立即执行。
为什么会这样?
谢谢。