关于ob_start()
ob_start()
仅仅说作用,不说怎么用,怎么用自己找,网上都有,今天只是帮有点模模糊糊的人清楚起来。
昨天看到朋友公司的代码就是类似上面这样的代码了,说白了没有任何意义的代码(我问了 不是为了setcookie header这类)。
我仔细想过之后,然后上网搜索了一下,发现相当多的初学者(技术上的初学者,不一定是PHP第一年生,有的人一辈子是初学者),并不理解ob的作用,只是网上经常把ob叫做输出缓冲,输出缓存,所以相当多的人就把ob系列函数当作是加快PHP页面显示的工具。
其实ob是output buffering的简称,而不是output cache,ob用对了,是能对速度有一定的帮助,但是盲目的加上ob函数,只会增加CPU额外的负担。下面我说说ob的基本作用。
1.防止在浏览器有输出之后再使用setcookie,或者header,session_start函数造成的错误。(我本以为最开始说的代码是这样的作用,但后来朋友说不是的),其实这样的用法少用为好,养成良好的代码习惯。
2.捕捉对一些不可获取的函数的输出,比如phpinfo会输出一大堆的HTML,但是我们无法用一个变量例如$info=phpinfo();来捕捉,这时候ob就管用了
3.对输出的内容进行处理,例如进行gzip压缩,例如进行简繁转换,例如进行一些字符串替换。
4.生成静态文件,其实就是捕捉整页的输出,然后存成文件,经常在生成HTML,或者整页缓存中使用。
对于刚才说的第三点中的GZIP压缩,可能是很多人想用,却没有真真用上的,其实稍稍修改下我朋友的代码,就可以实现页面的gzip压缩。
ob_start();
....
ob_clean();
这两句中间的任意输出,都可以用ob_get_contents();来获取而不输出到浏览器.
经典应用:将phpinfo()的信息写到文件里.
ob_start();
phpinfo();
$phpinfo = ob_get_contents();
//文件读写操作
ob_clean();

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can blur the photos in Yitian Camera, so how to blur the photos? Users can click Edit to select a photo, then click Effects and select Blur to blur the photo. This introduction to how to blur photos can tell you the specific content. The following is a detailed introduction, come and take a look! Yitian Camera usage tutorial. How to blur photos with Yitian Camera. Answer: Go to Edit-Special Effects-Blur. The specific process: 1. First open the app and click Edit on the lower left. 2. Then click on a picture and click the edit button. 3. Then click the special effects button below. 4. You can find the blur function below. 5. Then select the magnification of the blur.

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

What does a Bluetooth adapter do? With the continuous development of science and technology, wireless communication technology has also been rapidly developed and popularized. Among them, Bluetooth technology, as a short-distance wireless communication technology, is widely used in data transmission and connection between various devices. The Bluetooth adapter plays a vital role as an important device that supports Bluetooth communication. A Bluetooth adapter is a device that can turn a non-Bluetooth device into a device that supports Bluetooth communication. It realizes wireless connection and data transmission between devices by converting wireless signals into Bluetooth signals. Bluetooth adapter

Understand the role and usage of LinuxDTS In the development of embedded Linux systems, Device Tree (DeviceTree, DTS for short) is a data structure that describes hardware devices and their connection relationships and attributes in the system. The device tree enables the Linux kernel to run flexibly on different hardware platforms without modifying the kernel. In this article, the function and usage of LinuxDTS will be introduced, and specific code examples will be provided to help readers better understand. 1. The role of device tree device tree

The importance and role of the define function in PHP 1. Basic introduction to the define function In PHP, the define function is a key function used to define constants. Constants will not change their values during the running of the program. Constants defined using the define function can be accessed throughout the script and are global. 2. The syntax of define function The basic syntax of define function is as follows: define("constant name","constant value&qu

As a world-renowned sports brand, Nike's shoes have attracted much attention. However, there are also a large number of counterfeit products on the market, including fake Nike shoe boxes. Distinguishing genuine shoe boxes from fake ones is crucial to protecting the rights and interests of consumers. This article will provide you with some simple and effective methods to help you distinguish between real and fake shoe boxes. 1: Outer packaging title By observing the outer packaging of Nike shoe boxes, you can find many subtle differences. Genuine Nike shoe boxes usually have high-quality paper materials that are smooth to the touch and have no obvious pungent smell. The fonts and logos on authentic shoe boxes are usually clear and detailed, and there are no blurs or color inconsistencies. 2: LOGO hot stamping title. The LOGO on Nike shoe boxes is usually hot stamping. The hot stamping part on the genuine shoe box will show

PHP is a server-side scripting language widely used in web development. Its main function is to generate dynamic web content. When combined with HTML, it can create rich and colorful web pages. PHP is powerful. It can perform various database operations, file operations, form processing and other tasks, providing powerful interactivity and functionality for websites. In the following articles, we will further explore the role and functions of PHP, with detailed code examples. First, let’s take a look at a common use of PHP: dynamic web page generation: P

Title: Oracle Master Catalog: Concepts, Functions and Code Examples The master catalog (MasterCatalog) in the Oracle database is the basic directory structure of the database and is used to store metadata about database objects and other database information. The home directory plays the role of the management center of the database, recording information about all objects in the database, such as tables, indexes, views, users, etc., and also includes database configuration information and permission information. In Oracle database, the concept of home directory is very important, it is used
