-4

I understand the the header function is supposed to be used before any output is made which means things like echo , print , vardump. What i am wondering is will a header work after functions are executed. Say i wanted to insert data into a sql table or compare two variables.

Much appreciated.

MikanPotatos
  • 143
  • 1
  • 2
  • 9

1 Answers1

2

It's actually written pretty clearly in the manuel: header:

header() is used to send a raw HTTP header. See the » HTTP/1.1 specification for more information on HTTP headers.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>

This means, you can run any PHP code that doesn't produce any output before using header().

Community
  • 1
  • 1
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126