6

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.

Community
  • 1
  • 1
Tarik
  • 79,711
  • 83
  • 236
  • 349
  • It is not duplicate since I am asking more specific question with an example. – Tarik Jun 01 '11 at 05:03
  • 1
    It's foremost a newcomer coding style recommendation. It avoids that infamous error. But obviously no big deal if you know what you're doing. (Might be very sensible for distributed code still.) – mario Jun 01 '11 at 05:04
  • You're using more words, but you're not being more specific. You're asking the same question. – user229044 Jun 01 '11 at 12:37

4 Answers4

10

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.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • I've tried it and the code worked well except for this result `Slave.php\n` but it worked eventually... – Tarik Jun 01 '11 at 05:02
  • 1
    That single newline after `?>` gets eaten up by PHP. It's additional newlines and carriage returns or other whitespace which causes the error. (Well, same difference..) – mario Jun 01 '11 at 05:06
  • @mario Thanks, bad example, I'll update :) – alex Jun 01 '11 at 05:09
2

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.

David Chan
  • 7,347
  • 1
  • 28
  • 49
0

It is usually better to NOT end your script with closing PHP tag.

In your case a whitespace could remain after ?> (it is very sneaky and hard to tell where error is if something breaks because of this reason), so it would be considered as output and you won't be able to start session, for example or pass headers in case if you are developing a website.

Just my opinion. I likely never end my scripts with closing tag

Nemoden
  • 8,816
  • 6
  • 41
  • 65
0

The problem comes from having any number of line breaks other than 1: some php parsers get upset if there isn't a newline at that end, but if you have more than one, it is printed since anything outside php tags is considered HTML.

The most common problem is that a library/model file will have an extra line break, causing the headers to be sent long before the page/view is instantiated.

Bryan Agee
  • 4,924
  • 3
  • 26
  • 42