Home  >  Article  >  Backend Development  >  Detailed explanation of the key points of the PHP ob_start() function_PHP Tutorial

Detailed explanation of the key points of the PHP ob_start() function_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:32:50712browse

For example, you can use the setting information of the server and the client, but this information will be different depending on the client. If you want to save the output of the phpinfo() function What to do? Before there was no buffer control, it can be said that there was no solution at all, but with buffer control, we can easily solve it:

  1. < ; ?php
  2. ob_start(); //Open buffer
  3. phpinfo(); //Use phpinfo function
  4. $info=ob_get_contents(); //Get the contents of the buffer and assign it to $info
  5. $file =fopen('info.txt','w'); //Open the file info.txt
  6. fwrite($file,$info); //Write information to info.txt
  7. fclose($file); //Close the file info.txt
  8. ?>

Using the above method, you can save the phpinfo information of different users. This may not have been possible before! In fact, the above is a method to convert some "processes" into "functions"!

Some people may ask: "Is it just like this? Are there any other uses?" Of course, there is, for example, the PHP syntax highlighting in the author's forum is related to this (PHP's default syntax highlighting The function will output directly and cannot save the result. If it is displayed every time it is called, it will be a waste of CPU. The author's forum uses the method of controlling the buffer to retain the result displayed by the syntax highlighting function).

Maybe now you have a certain understanding of the function of PHP ob_start() function. The above example may seem simple, but in fact you have mastered the key points of using ob_start().

<1>. Use the PHP ob_start() function to open the browser's cache. This ensures that the contents of the cache will not be output before you call flush(), ob_end_flush() (or the program is executed).

<2>. Now you should know the advantages you have: you can use header, setcookie and session after any output content. This is a great feature of the PHP ob_start() function; you can also Use the parameters of ob_start, and then automatically run the command after the cache is written, such as ob_start("ob_gzhandler"); and our most common method is to use ob_get_contents() to get the contents of the cache, and then process it...

<3>. When the processing is completed, we can use various methods to output, flush(), ob_end_flush(), and automatic output after the program execution is completed. Of course, if you are using ob_get_contents(), then you have to control the output method yourself.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446112.htmlTechArticleFor example, you can use the setting information of the server and the client, but this information will be different due to different clients. If you want What should I do if I want to save the output of the phpinfo() function? Without relief...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn