Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    407
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Purpose of flush, ob_flush, etc.

    I have been coding php for a few years now but still never really understood all these functions. Can someone explain these a little bit clearer and a purpose to using them?

    I was checking out the documentation for them on php.net and just adding ob_start(); and ob_flush(); to the start and end of my index.php page (the main page on my site which is called on every page) cause significant speed increase on page loading (sooner and snappier). I know that faster loading on the user's end is always nice, but is this good to leave in, should i take it out, or is there something better to do?

    I'll continue to keep looking at the documentation but it would be nice to see some examples so I can know when to use these and make my code work better and smoother.

    Edit:
    I started taking out the two functions and putting them back in (toggling them) and I guess it's loading the same way, I might have to look at the time for it to see how long it's really taking.
    Last edited by Dubz; 04-06-2013 at 06:34 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I wouldn't expect output buffering to provide any substantial improvement with performance at all, unless there are a lot of errors in an existing structure where headers are being abandoned since they cannot be added to the stack.
    Output buffering's purpose is to store all output of the given block (or until end if there is no call to a flush), and then assemble the output headers and content prior to sending. This lets you cheat with headers and add anything header related after output (sessions, cookies, header calls, etc). Although in practicality, you can simply perform all of this work prior to any output anyway, so there isn't really a need for buffering.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    407
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Fou-Lu View Post
    I wouldn't expect output buffering to provide any substantial improvement with performance at all, unless there are a lot of errors in an existing structure where headers are being abandoned since they cannot be added to the stack.
    Yeah after toggling it in and out of my code I noticed it was just in my head. I guess since it isn't necessary then I won't use them then.

    On another note, do you have any recommendations as to cleaning out or releasing resources used in functions? I know after functions are finished any variables and data in them are unset and that, but is the memory freed up to or is it partially allocated to the script still (not sure what I even mean). Basically what I'm asking is after functions finish is there anything that would help free up more memory? I have a script that has a function which eval's another script when it's called (to keep more memory free by not having it loaded while it runs) and was wondering if there was anything else I could do to help free up memory.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You don't need to do anything to free them up. The EMALLOC call will detect when there are no more pointers to a variable, and will take the memory back. The functions themselves are a part of the stack memory, so that should make the free automatically forced upon termination of the function. So the memory is still allocated to PHP and may be reported as such with a get_memory_usage, but the memory itself is free for PHP to use as it sees fit. Garbage collection will clean up anything in heap or static memory as it detects the need for memory and variables without any valid pointers to them.
    As of PHP 5.3.0, you can force collection by issuing the gc_collect_cycles function. Although there is probably very few reasons to run a forced collections; image allocations and large file scans may require these as well as large recursive functions or loops.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    407
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Well considering it does loop for a while and more than one may be running I might have to look into that to keep the space cleaned up


  •  

    Posting Permissions

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