Hướng dẫn what is the use of ob_start () in php? - việc sử dụng ob_start() trong php là gì?

Điều này là để làm rõ thêm câu trả lời của JD Isaaks ...

Vấn đề bạn gặp phải thường xuyên là bạn đang sử dụng PHP để xuất HTML từ nhiều nguồn PHP khác nhau và các nguồn đó thường, vì bất kỳ lý do gì, xuất hiện thông qua các cách khác nhau.

Đôi khi bạn có nội dung HTML theo nghĩa đen mà bạn muốn trực tiếp xuất vào trình duyệt; Lần khác, đầu ra đang được tạo động [phía máy chủ].

Nội dung động luôn luôn [?] Sẽ là một chuỗi. Bây giờ bạn phải kết hợp HTML động được chuỗi này với bất kỳ HTML trực tiếp, trực tiếp đến hiện tại ... thành một cấu trúc nút HTML có ý nghĩa.

Điều này thường buộc nhà phát triển phải gói tất cả nội dung trực tiếp vào một chuỗi [như JD Isaak đang thảo luận] để nó có thể được phân phối/chèn đúng cách kết hợp với HTML động ... mặc dù bạn không thực sự muốn nó được bọc.

Nhưng bằng cách sử dụng các phương thức OB _ ##, bạn có thể tránh được mớ hỗn độn trong chuỗi đó. Thay vào đó, nội dung theo nghĩa đen là đầu ra cho bộ đệm. Sau đó, trong một bước dễ dàng, toàn bộ nội dung của bộ đệm [tất cả HTML theo nghĩa đen của bạn], được nối với chuỗi HTML động của bạn.

.



  
02 - component contents

Tracey tại Archive Dot org ¶

dan tại roteloftet dot com ¶Turn on output buffering

14 năm trước

Đồi Francois ¶[callable $callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS]: bool

Lucky760 tại Yahoo Dot Com ¶ob_get_contents[]. To output what is stored in the internal buffer, use ob_end_flush[]. Alternatively, ob_end_clean[] will silently discard the buffer contents.

Filip Dalge ¶

null8

Clancy Hood tại Gmail Dot Com ¶ob_start[] while another ob_start[] is active. Just make sure that you call ob_end_flush[] the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

Nếu bộ đệm đầu ra vẫn hoạt động khi tập lệnh kết thúc, PHP sẽ tự động xuất nội dung.

Thông số

callback

Một hàm callback tùy chọn có thể được chỉ định. Hàm này lấy một chuỗi làm tham số và sẽ trả về một chuỗi. Hàm sẽ được gọi khi bộ đệm đầu ra được xóa [đã gửi] hoặc làm sạch [bằng ob_flush [], ob_clean [] hoặc chức năng tương tự] hoặc khi bộ đệm đầu ra được xóa vào trình duyệt ở cuối yêu cầu. Khi callback được gọi, nó sẽ nhận được nội dung của bộ đệm đầu ra làm tham số của nó và dự kiến ​​sẽ trả về một bộ đệm đầu ra mới, kết quả sẽ được gửi đến trình duyệt. Nếu callback không phải là hàm có thể gọi được, hàm này sẽ trả về


  
02 - component contents
3. Đây là chữ ký gọi lại:ob_flush[], ob_clean[] or similar function] or when the output buffer is flushed to the browser at the end of the request. When callback is called, it will receive the contents of the output buffer as its parameter and is expected to return a new output buffer as a result, which will be sent to the browser. If the callback is not a callable function, this function will return

  
02 - component contents
3
. This is the callback signature:

Handler [Chuỗi


  
02 - component contents
4, int

  
02 - component contents
5 =?]: Chuỗi[string

  
02 - component contents
4
, int

  
02 - component contents
5 = ?
]: string


  
02 - component contents
6 Nội dung của bộ đệm đầu ra.

  
02 - component contents
7 Bitmask của hằng số

  
02 - component contents
8. Contents of the output buffer.

  
02 - component contents
7 Bitmask of

  
02 - component contents
8
constants.

Nếu callback trả về


  
02 - component contents
3 Đầu vào gốc được gửi đến trình duyệt.

  
02 - component contents
3
original input is sent to the browser.

Tham số callback có thể được bỏ qua bằng cách truyền giá trị null.null value.

ob_end_clean [], ob_end_flush [], ob_clean [], ob_flush [] và ob_start [] có thể không được gọi từ hàm gọi lại. Nếu bạn gọi họ từ chức năng gọi lại, hành vi không được xác định. Nếu bạn muốn xóa nội dung của bộ đệm, hãy trả về "" [chuỗi null] khỏi hàm gọi lại. Bạn thậm chí không thể gọi các chức năng bằng cách sử dụng các chức năng bộ đệm đầu ra như


It's like comparing oranges to oranges.

3 hoặc

It's like comparing oranges to oranges.

4 từ chức năng gọi lại., ob_end_flush[], ob_clean[], ob_flush[] and ob_start[] may not be called from a callback function. If you call them from callback function, the behavior is undefined. If you would like to delete the contents of a buffer, return "" [a null string] from callback function. You can't even call functions using the output buffering functions like

It's like comparing oranges to oranges.

3 or

It's like comparing oranges to oranges.

4 from a callback function.

Ghi chú::

Hàm ob_gzHandler [] tồn tại để tạo điều kiện gửi dữ liệu được mã hóa GZ đến các trình duyệt web hỗ trợ các trang web nén. ob_gzHandler [] xác định loại nội dung mã hóa trình duyệt sẽ chấp nhận và sẽ trả về đầu ra của nó cho phù hợp. function exists to facilitate sending gz-encoded data to web browsers that support compressed web pages. ob_gzhandler[] determines what type of content encoding the browser will accept and will return its output accordingly.


It's like comparing oranges to oranges.

5

Nếu tham số tùy chọn


It's like comparing oranges to oranges.

5 được truyền, bộ đệm sẽ được xóa sau khi bất kỳ cuộc gọi đầu ra nào khiến chiều dài của bộ đệm bằng hoặc vượt quá

It's like comparing oranges to oranges.

5. Giá trị mặc định

It's like comparing oranges to oranges.

8 có nghĩa là hàm đầu ra sẽ chỉ được gọi khi đóng bộ đệm đầu ra.


It's like comparing oranges to oranges.

9

Tham số


It's like comparing oranges to oranges.

9 là một bitmask kiểm soát các hoạt động có thể được thực hiện trên bộ đệm đầu ra. Mặc định là cho phép các bộ đệm đầu ra được làm sạch, xả và loại bỏ, có thể được đặt rõ ràng thông qua $callback1 | $callback2 | $callback3, hoặc PHP_OUTPUT_HANDLER_STDFLAGS như tốc ký.$callback1 | $callback2 | $callback3, or PHP_OUTPUT_HANDLER_STDFLAGS as shorthand.

Mỗi cờ kiểm soát truy cập vào một tập hợp các chức năng, như được mô tả dưới đây:

Không thay đổiChức năng
$callback1 ob_clean [], ob_end_clean [] và ob_get_clean []., ob_end_clean[], and ob_get_clean[].
$callback2 ob_end_flush [], ob_flush [] và ob_get_flush []., ob_flush[], and ob_get_flush[].
$callback3 ob_end_clean [], ob_end_flush [] và ob_get_flush []., ob_end_flush[], and ob_get_flush[].

Trả về giá trị

Trả về $callback8 khi thành công hoặc


  
02 - component contents
3 về thất bại.$callback8 on success or

  
02 - component contents
3
on failure.

Ví dụ

Ví dụ #1 Chức năng gọi lại do người dùng xác định

null0

null1

null2

Ví dụ trên sẽ xuất ra:


It's like comparing oranges to oranges.

Ví dụ #2 Tạo bộ đệm đầu ra không thể xác định được

null3

null4

null5

Xem thêm

  • ob_get_contents [] - Trả về nội dung của bộ đệm đầu ra
  • OB_END_CLEAN [] - Sạch [xóa] Bộ đệm đầu ra và tắt bộ đệm đầu ra
  • ob_end_flush [] - Flush [Gửi] Bộ đệm đầu ra và tắt bộ đệm đầu ra
  • ob_implicit_flush [] - Bật/tắt hết lần bật/tắt
  • ob_gzHandler [] - hàm gọi lại ob_start thành bộ đệm đầu ra gzip
  • ob_iconv_handler [] - Chuyển đổi mã hóa ký tự làm bộ đệm bộ đệm đầu ra
  • mb_output_handler [] - chức năng gọi lại chuyển đổi mã hóa ký tự trong bộ đệm đầu ra
  • ob_tidyHandler [] - chức năng gọi lại ob_start để sửa chữa bộ đệm

Ray Paseur [Paseur ... tưởng tượngB.com] ¶

17 năm trước

null6

null7

null8

ed.oohay [a] Suamhcs_rodnan ¶

19 năm trước

null9

$chunk_size0

$chunk_size1

$chunk_size2

net_navard tại yahoo dot com

16 năm trước

$chunk_size3

$chunk_size4

$chunk_size5

$chunk_size6

$chunk_size7

$chunk_size8

$chunk_size9

null8

mjr ¶

18 năm trước

$flags1

McHojrin tại gmail dot com

10 năm trước

$flags2

$flags3

$flags4

$flags5

null8

Asher Haig [Ahaig tại RidiculousPower Dot Com] ¶

15 năm trước

$flags7

$flags8

$flags9

null8

jhlavon ¶

9 năm trước

PHP_OUTPUT_HANDLER_STDFLAGS1

PHP_OUTPUT_HANDLER_STDFLAGS2

PHP_OUTPUT_HANDLER_STDFLAGS3

null8

Mbutscher tại GMX Dot de ¶

6 năm trước

PHP_OUTPUT_HANDLER_STDFLAGS5

PHP_OUTPUT_HANDLER_STDFLAGS6

PHP_OUTPUT_HANDLER_STDFLAGS7

PHP_OUTPUT_HANDLER_STDFLAGS8

Chris ¶

12 năm trước

PHP_OUTPUT_HANDLER_STDFLAGS9

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]0

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]1

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]2

null8

Ernest tại Vogelsinger Dot tại ¶

16 năm trước

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]4

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]5

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]6

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]7

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]8

chdir[dirname[$_SERVER['SCRIPT_FILENAME']]]9

null8

mjr ¶

18 năm trước

callback1

callback2

callback3

null8

McHojrin tại gmail dot com

15 năm trước

callback5

callback6

callback7

null8

jhlavon ¶

9 năm trước

callback9


  
02 - component contents
00


  
02 - component contents
01


  
02 - component contents
02

null8

Mbutscher tại GMX Dot de ¶

15 năm trước


  
02 - component contents
04


  
02 - component contents
05

null8

jhlavon ¶

16 năm trước


  
02 - component contents
07


  
02 - component contents
08


  
02 - component contents
09


  
02 - component contents
10


  
02 - component contents
11


  
02 - component contents
12


  
02 - component contents
13

Filip Dalge ¶

10 năm trước


  
02 - component contents
14


  
02 - component contents
15


  
02 - component contents
16


  
02 - component contents
17

null8

Clancy Hood tại Gmail Dot Com ¶

13 năm trước


  
02 - component contents
19


  
02 - component contents
20


  
02 - component contents
21

Charlie Farrow ¶

15 năm trước


  
02 - component contents
22


  
02 - component contents
23


  
02 - component contents
24

null8

Quản trị viên tại Bobfrank Dot org ¶

17 năm trước


  
02 - component contents
26


  
02 - component contents
27


  
02 - component contents
28


  
02 - component contents
29

null8

bty-adminf2 tại trbly dot net ¶

7 năm trước


  
02 - component contents
31


  
02 - component contents
32


  
02 - component contents
33


  
02 - component contents
34


  
02 - component contents
35


  
02 - component contents
36

null8

Aaron tại OffTone.com ¶

18 năm trước


  
02 - component contents
38


  
02 - component contents
39


  
02 - component contents
40

null8

tôi tại haravikk dot com ¶

10 năm trước


  
02 - component contents
42


  
02 - component contents
43


  
02 - component contents
44

null8

null8

Clancy Hood tại Gmail Dot Com ¶


  
02 - component contents
46


  
02 - component contents
47


  
02 - component contents
48


  
02 - component contents
49


  
02 - component contents
50

null8

13 năm trước

Charlie Farrow ¶


  
02 - component contents
52


  
02 - component contents
53

null8

13 năm trước

17 năm trước


  
02 - component contents
55


  
02 - component contents
56


  
02 - component contents
57


  
02 - component contents
58

null8

bty-adminf2 tại trbly dot net ¶

17 năm trước


  
02 - component contents
60


  
02 - component contents
61


  
02 - component contents
62


  
02 - component contents
63

null8

bty-adminf2 tại trbly dot net ¶

17 năm trước


  
02 - component contents
65


  
02 - component contents
66


  
02 - component contents
67


  
02 - component contents
68

bty-adminf2 tại trbly dot net ¶

13 năm trước


  
02 - component contents
69


  
02 - component contents
70


  
02 - component contents
71


  
02 - component contents
72

Charlie Farrow ¶

13 năm trước


  
02 - component contents
73


  
02 - component contents
74


  
02 - component contents
75


  
02 - component contents
76


  
02 - component contents
76


  
02 - component contents
78


  
02 - component contents
79

null8

Charlie Farrow ¶

13 năm trước


  
02 - component contents
81


  
02 - component contents
82

null8

Charlie Farrow ¶

15 năm trước


  
02 - component contents
84


  
02 - component contents
85


  
02 - component contents
86


  
02 - component contents
87

Quản trị viên tại Bobfrank Dot org ¶

15 năm trước


  
02 - component contents
88


  
02 - component contents
89


  
02 - component contents
90

null8

Quản trị viên tại Bobfrank Dot org ¶

16 năm trước


  
02 - component contents
92


  
02 - component contents
93


  
02 - component contents
94


  
02 - component contents
95

null8

17 năm trước

16 năm trước


  
02 - component contents
97

bty-adminf2 tại trbly dot net ¶

17 năm trước


  
02 - component contents
98


  
02 - component contents
99

null8

bty-adminf2 tại trbly dot net ¶

17 năm trước


It's like comparing oranges to oranges.

01


It's like comparing oranges to oranges.

02


It's like comparing oranges to oranges.

03


It's like comparing oranges to oranges.

04

null8

13 năm trước

Charlie Farrow ¶


It's like comparing oranges to oranges.

06


It's like comparing oranges to oranges.

07


It's like comparing oranges to oranges.

08


It's like comparing oranges to oranges.

09


It's like comparing oranges to oranges.

10


It's like comparing oranges to oranges.

11


It's like comparing oranges to oranges.

12

null8

15 năm trước

15 năm trước


It's like comparing oranges to oranges.

14


It's like comparing oranges to oranges.

15


It's like comparing oranges to oranges.

16


It's like comparing oranges to oranges.

17


It's like comparing oranges to oranges.

18

null8

Khi nào tôi nên sử dụng ob_start?

Sử dụng ob_start cho phép bạn giữ nội dung trong bộ đệm phía máy chủ cho đến khi bạn sẵn sàng hiển thị nó.Điều này thường được sử dụng để các trang có thể gửi các tiêu đề 'sau khi' chúng 'đã gửi' một số nội dung [tức là quyết định chuyển hướng một nửa thông qua kết xuất một trang].allows you to keep the content in a server-side buffer until you are ready to display it. This is commonly used to so that pages can send headers 'after' they've 'sent' some content already [ie, deciding to redirect half way through rendering a page].

Sử dụng gì của ob_end_clean trong PHP?

Hàm ob_end_clean [] xóa bộ đệm đầu ra cao nhất và tất cả các nội dung của nó mà không gửi bất cứ thứ gì đến trình duyệt.deletes the topmost output buffer and all of its contents without sending anything to the browser.

Việc sử dụng ob_clean là gì?

Định nghĩa và sử dụng hàm ob_clean [] xóa tất cả các nội dung của bộ đệm đầu ra trên cùng, ngăn chúng không được gửi đến trình duyệt.deletes all of the contents of the topmost output buffer, preventing them from getting sent to the browser.

Bộ đệm đầu ra PHP là gì?

Bộ đệm đầu ra là một cơ chế để kiểm soát số lượng dữ liệu đầu ra [không bao gồm các tiêu đề và cookie] PHP nên giữ nội bộ trước khi đẩy dữ liệu đó đến máy khách.Nếu đầu ra của ứng dụng của bạn vượt quá cài đặt này, PHP sẽ gửi dữ liệu đó theo các khối có kích thước gần như kích thước bạn chỉ định.a mechanism for controlling how much output data [excluding headers and cookies] PHP should keep internally before pushing that data to the client. If your application's output exceeds this setting, PHP will send that data in chunks of roughly the size you specify.

Bài Viết Liên Quan

Chủ Đề