ob_start() , ob_end_flush() 함수 사용
예를 들어, 페이지 로딩시간(php 파일 실행시간)을 출력하려 한다고 해 보자.
파일의 처음에는 다음과 같은 코드가 들어간다.
$time_start = microtime();
그리고, 파일의 맨 끝에 다시 한 번 microtime()값을 구해서
차이값을 출력하면 실행시간을 출력할 수 있다.
하지만, 여기서 문제.
만일, 웹페이지의 타이틀(<title> </title>)에 넣고 싶다면?
이럴 때 필요한 함수가 ob_로 시작하는 함수다.
ob_start
ob_end_flush
ob_start 함수를 만나게 되면
이후의 출력분을 바로 출력하지 않고
버퍼(buffer)에 저장한다.
ob_end_flush 함수를 만나서야 비로소
그동안 버퍼에 쌓여있는 데이터를 뿌린다.
아래는 이를 이용한 간단한 소스다.
<?php
function _get_microtime()
{
list($usec, $sec)=explode(' ',microtime());
return (float)$usec+(float)$sec;
}
$time_start=_get_microtime();
function stringReplace ( $a )
{
return ( str_replace( 'the_time_elapsed' , _get_mitcotime()-$time_start , $a ) ) ;
}
ob_start( 'stringReplace' );
?>
<html>
<head>
<title>the_time_elapsed</title>
</head>
<body>
<p>페이지 출력 소요시간을 타이틀에 넣습니다.</p>
<p>이 페이지 출력 소요시간은 the_time_elapsed초입니다.</p>
</body>
</html>
<?php
ob_end_flush();
?>