PHP closing tag

C++, C#, Java, PHP, ect...
Post Reply
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

PHP closing tag

Post 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 :)
User avatar
xcalpro
Posts: 65
Joined: Sat May 19, 2012 10:25 pm

Re: PHP closing tag

Post 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.
Skills: HTML5, JavaScript, PHP, SQL, Python, BASIC, HeroEngine(HSL), AGS(Lua), Unity, Photon, 3D Max, Mudbox, Photoshop, Poser, Flash
"Jack-of-all-Trades, Master of none"
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP closing tag

Post by Chris »

No need to close it off as long as there is nothing needed afterwards, for obvious reasons.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PHP closing tag

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP closing tag

Post 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).
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: PHP closing tag

Post by Winawer »

In my opinion it's better not to close the PHP tag so you don't get extra whitespace.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP closing tag

Post 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. ;)
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: PHP closing tag

Post 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.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PHP closing tag

Post by Xaleph »

headers already sent.. That`s what you get when using the closing tag and accidentally add a whitespace.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP closing tag

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “Coding”