RC.
RC.

Reputation: 31

PHP Output Buffering

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!

Upvotes: 3

Views: 510

Answers (3)

davethegr8
davethegr8

Reputation: 11595

It would redirect to page.php without* processing the rest of the commands.

*Technically, execution continues on past the header call unless you specifically stop it after (die, exit). You'll never notice this if you are just setting variables and displaying stuff, but if you have commands that change a database, it can be very difficult to find out where those changes are coming from.

Upvotes: 1

Doug Neiner
Doug Neiner

Reputation: 66191

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.

Upvotes: 2

Mike B
Mike B

Reputation: 32145

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.

Upvotes: 8

Related Questions