PHP ob_start()

PHPz
풀어 주다: 2024-08-29 12:50:04
원래의
566명이 탐색했습니다.

PHP 프로그래밍 언어의 ob_start() 함수는 언급된 특정 스크립트의 HTML 및 모든 유형의 에코 이전에 특정 출력의 버퍼링을 활성화하는 데 도움이 됩니다. 우리 모두는 PHP가 해석된 웹 개발 프로그래밍 언어 중 하나이므로 프로그램의 모든 명령문이 하나씩 실행된다는 것을 알고 있습니다. 따라서 PHP는 일부 청크로 HTML을 웹 브라우저에 보내는 데 도움을 주어 성능을 저하시키는 데 도움이 됩니다. 출력 버퍼링의 도움으로 생성된 HTML은 마지막 PHP 스크립트 실행 후 버퍼에 저장됩니다. 이를 극복하기 위해 PHP의 ob_start()가 탄생했습니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

PHP ob_start()의 구문 및 매개변수

ob_start();
로그인 후 복사

PHP 프로그래밍 언어의 ob_start() 함수는 다양한 선택적 매개변수를 허용합니다. 그들은:

  1. 콜백 기능
  2. 청크 크기
  3. 플래그.

1. 콜백 함수: 콜백 함수 매개변수는 일반적으로 출력 버퍼의 내용을 가져온 다음 렌더링을 위해 브라우저에 특별히 전송될 문자열을 반환하는 함수를 기대하는 데 도움이 됩니다. 일반적으로 HTML 콘텐츠를 압축하는 데 사용되는 콜백 함수 매개변수입니다. ob_start() 함수의 선택적 매개변수입니다.

2. 청크 크기: ob_start() 함수의 청크 크기 매개변수도 또 다른 선택적 매개변수이며 출력 버퍼 크기를 설정하는 데 도움이 되며 버퍼가 초과되거나 가득 차면 출력됩니다.

3. 플래그: PHP 프로그래밍 언어의 ob_start() 함수의 플래그 매개변수는 특정 출력 버퍼에 구현될 일부 작업을 제어하기 위해 비트마스크를 허용하는 데 도움이 됩니다. 플래그 매개변수는 제한된 액세스를 전달하는 데 도움이 되며 기본 권한은 정리 및 버퍼 제거에 대한 액세스를 제공하는 데 도움이 됩니다. 이 매개변수는 다른 두 매개변수와 마찬가지로 선택적 매개변수이기도 합니다.

PHP의 ob_start() 함수 반환 유형:

ob_start() 함수는 성공 출력 시 TRUE 값을 반환하는 데 도움이 됩니다. 그렇지 않으면 출력 반환으로 False가 반환됩니다.

PHP에서 ob_start()는 어떻게 작동하나요?

PHP 프로그래밍 언어의 ob_start()는 PHP 스크립트의 일부 HTML 콘텐츠 유형에서 에코가 발생하기 전에 출력 버퍼/버퍼링을 활성화하는 데 도움이 됩니다. ob_start() 함수는 구체적으로 어떤 매개변수도 허용하지 않지만 일부 선택적 매개변수를 허용하여 작동합니다. 콜백 매개변수, 청크 크기 매개변수, 플래그 매개변수가 있습니다. 이 ob_start()는 PHP 4, PHP 5 및 PHP 7 버전에서만 작동합니다. 출력 버퍼링을 켜야만 작동합니다.

PHP ob_start() 함수 구현 예

다음은 PHP ob_start()의 예입니다.

예시 #1

ob_start() 함수의 콜백 기능을 이해하기 위해 PHP 프로그래밍 언어의 ob_start() 함수를 설명하는 예입니다. 여기에서는 먼저 PHP 태그가 열리고 매개변수가 있는 함수가 생성됩니다. 그런 다음 함수 내부에서 strtoupper() 함수와 함께 반환 함수를 사용하여 출력을 대문자로 반환합니다. 그런 다음 ob_start() 함수는 출력 변경에 도움이 되는 콜백 매개변수와 함께 사용됩니다. 여기서 출력은 echo 문의 도움으로 언급된 문자열입니다. 여기서 문자열은 "Hello Educba!!"입니다. 그러면 "HELLO EDUCBA!!"와 같은 대문자로 변경됩니다. 구문에서 무슨 일이 일어나고 있는지 이해할 수 있도록 출력을 확인하세요.

코드:

<?php
// This is PHP code which helps in illustrating the working
// of the ob_start() Function of PHP Language
function callback($buffer1){
// This function Returns Everything of output in CAPS.
return (strtoupper($buffer1));
}
ob_start("callback");
echo "Hello Educba!!";
?>
로그인 후 복사

출력:

PHP ob_start()

Example #2

This is also an example of illustrating the ob_start() function of the PHP Programming Language which helps in handling the output buffering. Here at first, inside of the PHP tags, a function called callback is created with the buffer1 as a parameter. Inside of the function str_replace() function is used which helps in returning the output of the output text just by replacing the required string text according to the need. Here mangoes and Pomegranates and the mangoes text will be replaced by the “Pomegranates” text. Then the function parenthesis are closed. Then ob_start() function is used with the callback parameter for the required return output. Then HTML tags are used. Inside the HTML and BODY tags, some string text is used. The string text can be a string or some paragraph that is actually mentioned based on our requirement. Her in the following text, the string text “mangoes” will be replaced with “Pomegranates”.

Code:

<?php
function callback($buffer1)
{
// This will help in replacing all Mangoes with the Pomegranates
return (str_replace("mangoes", "Pomegranates", $buffer1));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing the mangoes to Pomegranates.</p>
</body>
</html>
<?php
echo "<br>";
ob_end_flush();
?>
로그인 후 복사

Output:

PHP ob_start()

Example #3

This is an example of illustrating the ob_start() of the PHP Programming Language. Here at the first inside of the PHP tags, If the condition is created and then inside of it a function is mentioned as the condition and then the ob_start() function is used along with the callback parameter, chunk size parameter along with the flag parameters. If the IF condition is TRUE then the “Your PHP version is greater than or equal to PHP 5.4.0 version“ string text will be printed and if the IF condition is FALSE value then else condition statements will be printed. In ELSE also we used the ob_start() function is used with callback value as NULL, Chunk size parameter value as 0 and FALSE as the FLAG value. So this doesn’t produce any output. So to recognize this we used some string text with ECHO is used. PHP_OUTPUT_HANDLER_REMOVABLE is used to remove the output which is created by ob_start() just before the end of the script. PHP_OUTPUT_HANDLER_STDFLAG is the default set of some output buffer flags and it is equivalent to PHP_OUTPUT_HANDLER_REMOVABLE.

Code:

<?php
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^
PHP_OUTPUT_HANDLER_REMOVABLE);
echo "Your PHP version is greater than or equal to PHP 5.4.0 version";
} else {
ob_start(null, 0, false);
echo "Your PHP Version is less than PHP 5.4.0 version";
}
?>
로그인 후 복사

Output:

PHP ob_start()

Conclusion

I hope you learned what is the definition of ob_start of the PHP Programming Language along with its syntax and explanations, How the ob_start() function works in PHP along with various examples of ob_start() function to understand the ob_start() better and so easily.

Recommended Article

This is a guide to the PHP ob_start(). Here we discuss the introduction, syntax, and working of the ob_start() function in PHP along with different examples and code implementation. You can also go through our other suggested articles to learn more –

  1. Overview of Abstract Class in Python
  2. What is Abstract Class in PHP?
  3. Socket Programming in PHP with Methods
  4. PHP chop() | How to Work?

위 내용은 PHP ob_start()의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!