Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Output Buffering

Tags:

php

Simple question:

If I enable output buffering...

ob_start();
  $a = true;
  header('Location: page.php'); 
  $a = false;
ob_end_flush();

... will $a be registered as false, or will it just redirect the page without processing the command (as it would if output buffering were not enabled)?

Thanks!

like image 320
RC. Avatar asked Sep 14 '26 16:09

RC.


2 Answers

Unless you call exit() or die() after the header redirect, $a will be false as the rest of the page continues to parse (with or without the buffering).

Unless you have a special reason, header("Location: ..."); should always be followed by one of the above functions as to not waste cpu cycles or memory.

like image 154
Mike B Avatar answered Sep 16 '26 07:09

Mike B


Output buffering does exactly what the name infers, nothing more. It only buffers the output, not variable assignment or object state. In this case $a would be set to false at the end of the code sample you provided. What happens after that is up to your code execution.

like image 38
Doug Neiner Avatar answered Sep 16 '26 08:09

Doug Neiner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!