PHP에서 출력 버퍼링은 재미 있습니다. 그것은 많은 것들을 단순화합니다. ob_start()는 스크립트의 맨 위에서 사용하고 ob_get_clean() (또는 다른 함수)은 맨 아래에 사용합니다.출력 버퍼링, 계층 적?
두 호출 사이에 부모 호출을 간섭하지 않고 해당 함수를 다시 호출 할 수 있습니다.
이 코드 유형이 유효합니까? (잘 작동하지만 ...) 좋은 습관입니까?
<?php
ob_start(); //NOTICE !!!
echo '<p>echos of the top of the script</p>';
echo GetSomeOtherData(true);
echo '<p>echos after GetSomeOtherData()</p>';
$data = ob_get_clean(); //NOTICE !!!
echo $data;
//just a function to return something, with the help of output buffering
function GetSomeOtherData($toReturn)
{
ob_start(); //NOTICE !!!
echo '<p>This has been rendered inside a function</p>';
$function_data = ob_get_clean(); //NOTICE !!!
if($toReturn===true)
{
return $function_data;
}
else
{
//may be an error | return something else
return '<p>An Error</p>';
}
}
?>