2

Just like the question states, earlier today I solved a bug in my code by replacing a javascript redirect with a php redirect:

header('Location: index.php')

The only way I could accomplish this was by replacing every echo in my code with print. By doing this I was allowed to use the header() call.

I'm interested to know if this is bad practice, and if so, why?

Barmar
  • 741,623
  • 53
  • 500
  • 612
jrnxf
  • 744
  • 8
  • 22
  • 1
    Possible duplicate of [How are echo and print different in PHP?](http://stackoverflow.com/questions/234241/how-are-echo-and-print-different-in-php) – Xorifelse Nov 16 '16 at 23:08
  • The only difference, echo returns a void and print returns a bool. – Xorifelse Nov 16 '16 at 23:09
  • @Xorifelse: actually `print` returns `int`, specifically number 1, always. – Jirka Hrazdil Nov 16 '16 at 23:10
  • @JiriHrazdil While you are technically correct, how is it in "PHP" terms different from a bool? Do you do `if(print("") == 1)` or do you evaluate it as a bool by leaving out the `== 1`?. – Xorifelse Nov 16 '16 at 23:14
  • 2
    That should not have made a difference. `The only difference to print is that echo accepts an argument list.` -http://php.net/manual/en/function.echo.php both should cause output which makes the `header` not thrown. How about adding the code to the question so we can see what you are doing? – chris85 Nov 17 '16 at 00:04
  • 1
    There's no way that replacing `echo` with `print` could prevent the "Headers already sent" problem. Any form of output before `header()` causes that, it doesn't matter how it's done. You must have changed something else. – Barmar Nov 17 '16 at 00:15

1 Answers1

1

There is no difference between echo and print in this case.

Maybe you can try ob_clean to clear your php output buffer before you use header to send a redirect header

Fu Xu
  • 766
  • 2
  • 6
  • 21