Possible Duplicate:
Why do some scripts omit the closing php tag '?>'?
I've been reading some articles about Omitting Closing PHP tags since they say it is a good programming practice in PHP if your .php file doens't contain any other things. There are many questions like that but after I tried what they've done so far worked well on my machine. Maybe it is a fixed issue or something?
But I don't quite understand why it could be a good programming practice since it brings space or something but I've tried this one and works very well.
Master.php
<?php
echo "Master.php";
include "Slave.php";
header("Location:Slave.php");
?>
Slave.php
<?php
echo "Slave.php";
?>
I don't really quite get what the problem should be if I didn't use closing php tag.
Thanks.
If a file contains only PHP code, it is preferable to omit the PHP closing tag at the end of the file.
It is recommended that a closing PHP tag shall be omitted in a file containing only PHP code so that occurrences of accidental whitespace or new lines being added after the PHP closing tag, which may start output buffering causing uncalled for effects can be avoided.
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.
The main issue is you may include additional whitespace (but it can be any chars) after the closing ?>
(besides one \n
which PHP allows, thanks Mario).
This extra whitespace appears to PHP as output to be sent. This makes PHP start sending the response body, therefore making any additional headers being set/modified impossible.
This is hard to debug (as whitespace is generally invisible in text editors) and often the cause of the dreaded Headers already sent error.
the problem with the closing tag is that any whitespace after the last ?> may cause bugs and is very difficult to detect while bug fixing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With