目录

PHP 输出控制函数


PHP 输出控制函数

PHP 提供了一组函数来控制将哪些内容发送到浏览器以及何时发送。这被称为输出控制

输出可以来自以下任意来源:

  • echo,print,printf,print_r...以及其他类似的函数和语句
  • PHP 的注意事项、警告和错误
  • 任何超出范围的内容<?php ?>标签

PHP 及其运行的后端可能会在将输出发送给用户之前将其保存在缓冲区中。

笔记:输出控制函数可以创建任意数量的输出缓冲区。输出缓冲区捕获程序给出的输出。每个新的输出缓冲区都放置在输出缓冲区堆栈的顶部,它提供的任何输出都将被其下面的缓冲区捕获。输出控制函数仅处理最顶层的缓冲区,因此必须删除最顶层的缓冲区才能控制其下面的缓冲区。

安装

PHP 输出控制函数是 PHP 核心的一部分。无需安装即可使用这些功能。


运行时配置

输出控制函数的行为受 php.ini 中的设置影响:

Name Default Description Version
output_buffering "0" Enables output buffering for all PHP files by default 4
output_handler NULL Set the name of the default function which handles the output of all output buffers 4
implicit_flush "0" Enables implicit flush, which causes output to be sent directly to the browser on each output statement 4
url_rewriter.tags "a=href,area=href, frame=src,form=,fieldset=" Indicates which HTML tags and attributes can be modified by the URL rewriter (the output_add_rewrite_var() function.) 4.3
url_rewriter.hosts The current value of $_SERVER['HTTP_HOST'] URL rewriting is only done on the server's own URLs by default. To allow for rewriting URLs of other websites, set the hostnames of the other websites here. 7.1

PHP 输出控制函数

Method Function
flush() Attempts to send content from the system's output buffer to the browser
ob_clean() Deletes all of the content from the topmost output buffer
ob_end_clean() Deletes the topmost output buffer and all of its contents
ob_end_flush() Deletes the topmost output buffer and outputs its contents
ob_flush() Outputs the contents of the topmost output buffer and clears the buffer
ob_get_clean() Returns all of the contents of the topmost output buffer and clears the buffer
ob_get_contents() Returns the contents of the topmost output buffer
ob_get_flush() Outputs and returns the contents of the topmost output buffer and then deletes the buffer
ob_get_length() Returns the number of bytes of data that are in the topmost output buffer
ob_get_level() Returns a number indicating how many output buffers are on the stack
ob_get_status() Returns information about the output buffers
ob_gzhandler() Used as a callback function for ob_start() to compress the contents of the buffer when sending it to the browser
ob_implicit_flush() Turns implicit flushing on or off
ob_list_handlers() Returns an array of callback function names that are being used by the topmost output buffer
ob_start() Creates a new output buffer and adds it to the top of the stack
output_add_rewrite_var() Used to append query string parameters to any URL in the output
output_reset_rewrite_vars() Removes all variables added by output_add_rewrite_var()