Home arrow PHP arrow Page 4 - Output Buffering

Further suggestions - PHP

Output control (or output buffering) allows you to write and execute your scripts normally but send data to the web browser at selected times. The main benefit of this system is that you can call the header(), setcookie() and session_start() functions at any place in your scripts without having to worry about the "headers already sent" error message.

TABLE OF CONTENTS:
  1. Output Buffering
  2. Login Script
  3. Code Explained
  4. Further suggestions
By: David Web
Rating: starstarstarstarstar / 4
September 02, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

You can set the maximum buffer size in your PHP configuration file. The default is 4096 characters. Below is an extract from a PHP configuration file, showing the default setting:


; - log_errors = On [Security]

; This directive complements the above one. Any errors that occur during the

; execution of your script will be logged (typically, to your server's error log,

; but can be configured in several ways). Along with setting display_errors to off,

; this setup gives you the ability to fully understand what may have gone wrong,

; without exposing any sensitive information to remote users.

; - output_buffering = 4096 [Performance]

; Set a 4KB output buffer. Enabling output buffering typically results in less

; writes, and sometimes less packets sent on the wire, which can often lead to

; better performance. The gain this directive actually yields greatly depends

; on which Web server you're working with, and what kind of scripts you're using.




You can send compressed output to browsers by starting output control with ob_start('ob_gzhandler’), minimizing the download size of your pages. The ob_gzhandler() function determines the appropriate type of compression to use (gzip,deflate, or none) based on what the browser can accept. Compression is beneficial only on larger web pages, and in order to use it, the output_buffering setting in thephp.inifile must be set tooff.

The ob_get_length() function returns the length of the current buffer contents.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>


<body>


<?php


ob_start();


echo "Go for it ";


$oblength1 = ob_get_length();


echo "World";


$oblength2= ob_get_length();


ob_end_clean();


echo $oblength1 . ", ." . $oblength2;

var_dump($oblength1, $oblength2);

?>


</body>

</html>


The above code outputs the following:

10, .15

int(10)

int(15)

The ob_get_contents() function returns the current buffer so that it can be assigned to a variable if needed:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>


<body>


<?php


ob_start();


echo "My name is David Web ";


$obcontents1 = ob_get_contents();


echo "and i'm very tired right now";


$obcontents2 = ob_get_contents();


ob_end_clean();


var_dump($obcontents1, $obcontents2);

?>


</body>

</html>


The code above outputs:

string(21) "My name is David Web "

string(49) "My name is David Web and i'm very tired right now"

PHP automatically runs the ob_end_flush() function at the end of a script if it is not otherwise called. But it is better to get into the habit of calling it yourself.



 
 
>>> More PHP Articles          >>> More By David Web
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: