Page 1 of 1

PHP closing tag

Posted: Tue Jun 26, 2012 4:18 pm
by OldRod
Started messing around with CodeIgniter and was looking through some docs on their site. Ran across this:

http://codeigniter.com/user_guide/gener ... losing_tag
PHP Closing Tag

The PHP closing tag on a PHP document ?> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. For this reason, all PHP files should OMIT the closing PHP tag, and instead use a comment block to mark the end of file and it's location relative to the application root. This allows you to still identify a file as being complete and not truncated.

INCORRECT:

Code: Select all

<?php

echo "Here's my code!";

?>
CORRECT:

Code: Select all

<?php

echo "Here's my code!";

/* End of file myfile.php */
/* Location: ./system/modules/mymodule/myfile.php */
I'd never heard of this before... passing it along since I know there are a lot of PHP users here :)

Re: PHP closing tag

Posted: Tue Jun 26, 2012 5:04 pm
by xcalpro
I believe this is only specific to CodeIgniter since it is handling your PHP code. As a general rule you should close your PHP tag. The idea of leaving server-side code open just scares me.

Re: PHP closing tag

Posted: Tue Jun 26, 2012 5:31 pm
by Chris
No need to close it off as long as there is nothing needed afterwards, for obvious reasons.

Re: PHP closing tag

Posted: Tue Jun 26, 2012 7:07 pm
by Xaleph
You don`t need to close it, as long as your script is PHP only, if you do mix in HTML, you should obviously use the closing tag. It`s not specific for CI.

Re: PHP closing tag

Posted: Wed Jun 27, 2012 4:47 am
by Jackolantern
It is a best practice to close your tags. However, as you pointed out, closing tags are not only not necessary in CodeIgniter, they can cause problems. This is because CI is a framework. You could basically say that CI is its own application that calls your scripts as needed. That is the main difference between a library and a framework: you call library code in your application, whereas a framework calls your code in its application (that does happen to be your application, too, of course; it is just that CI and other frameworks are basically self-standing applications as soon as they are unzipped. They just don't do much).

Re: PHP closing tag

Posted: Wed Jun 27, 2012 5:10 am
by Winawer
In my opinion it's better not to close the PHP tag so you don't get extra whitespace.

Re: PHP closing tag

Posted: Wed Jun 27, 2012 5:32 am
by Jackolantern
Whitespace is of little importance on the server-side. You really only worry about whitespace on the client-side since every character must be transmitted over the internet (although minification can deal with that nicely). It was never about performance, only transmittance.

Of course, if it is your personal preference to not like the whitespace from a style view point, that is obviously fine. I personally like the symmetry of having an opening and a closing tag lol. ;)

Re: PHP closing tag

Posted: Wed Jun 27, 2012 6:16 am
by Winawer
The whitespace after the closing tag gets sent to the client, so it's a client-side issue too. Also, extra whitespace can lead to errors in some cases. Not closing the tag means there's no need to even think about this stuff, the errors/issues just get removed in all cases.

Re: PHP closing tag

Posted: Wed Jun 27, 2012 7:36 pm
by Xaleph
headers already sent.. That`s what you get when using the closing tag and accidentally add a whitespace.

Re: PHP closing tag

Posted: Wed Jun 27, 2012 8:35 pm
by Jackolantern
I've heard that before, but never understood how someone can add whitespace after the closing tag. I have written hundreds of PHP scripts, and never did it once.

There are probably reasons not to add it, but it just bugs me to death. Just some kind of developer OCD lol.