SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use of ob_start()

    First of all I am quite new to PHP. I have some doubts regarding the use of ob_start() function.

    According to PHP manual This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.


    So when I use this code below I should not get any output as I have turned ON the output buffering.

    But I do get the output. So what is the actual use of output buffering?

    Code:
    <?php
    ob_start();
    for($i=1;$i<=4;$i++)
    {
    	echo $i;echo "<br>";
    	sleep(1);
    }
    ?>
    According to the manual, I should get the output only if I use ob_flush() or ob_end_clean().

    Kindly suggest.

  2. #2
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it automatically flushes at the end of the script. If you don't want any output, call ob_end_clean at the end of your script.

    It's useful because it waits until the end of the script to send out everything.

    Normally, this would throw an error:
    PHP Code:
    <?php

    echo 'Your favorite number is ';

    if(!isset(
    $_POST['num']))
    {
         
    header('Location: lol.php');
    }

    echo 
    $_POST['num'];

    ?>
    With output buffering, you can do it:
    PHP Code:
    <?php

    ob_start
    ();

    echo 
    'Your favorite number is ';

    if(!isset(
    $_POST['num']))
    {
         
    header('Location: lol.php');
    }

    echo 
    $_POST['num'];

    ?>
    EDIT: That's a pretty bad example if you couldn't tell.

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Young thanks for the explanation.

    However the code below (without ob_start()) also produce the same effect i.e. it displays all the numbers together at the end of the script like this 1,2,3,4,5,

    Code:
    <?php
    for($i=1;$i<=5;$i++)
    {
    	echo $i. ",";
    	sleep(1);
    }
    ?>

    If we are NOT using ob_start(), it should display the numbers one at a time like this:

    Code:
    1,
    
    1,2,
    
    1,2,3,
    
    1,2,3,4,
    
    1,2,3,4,5, [final output]
    Sorry but I am still confused with this particular example. I mean why the script is still holding the output even when we are NOT using ob_start()?

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even when output buffering is off PHP and the webserver try to make intellegent descissions as to when to send TCP/IP packets to the client. Sending 1 byte per packet is not very intellegent. To override this behaviour you need to flush the output.
    PHP Code:
    <?php
    for($i=1;$i<=4;$i++)
    {
        echo 
    "$i<br>";
        
    flush();
        
    sleep(1);
    }
    ?>

  5. #5
    SitePoint Member
    Join Date
    Nov 2004
    Location
    USA
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Headers already sent

    Utilizing ob_start(); will correct any "headers already sent" errors that you may receive in your code, this most often occurs when you use the header(); function to re-direct to another page or what have you. All output from the code will be buffered until all code has been processed, then will be displayed.

  6. #6
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now use of ob_start() is quite clear to me. Thanks guys for the wonderful explanations.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •