Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShafeeq Ahmed
6,058 PointsClosing PHP block
I did not close my PHP block in my file. but the code still runs. There are no HTML code in my file.
Can anyone explain this to me. I mean is there any theory behind it. I don't want to miss anything in PHP when I learn it?
2 Answers
Kai Nightmode
37,045 PointsIn your PHP file's case, not closing with ?> has some benefits.
- PHP will run just fine. You can think of it auto-closing that block of PHP code.
- Saves you from typing two characters and arguably looks prettier.
- Saves you from accidentally serving HTML white space when you didn't mean to.
Let's take a look at that last point in more detail.
Say you had a PHP file that you were requiring like the following.
<?php
// startup.php
?>
<!-- oh oh extra white space before this comment -->
Notice the extra white space above. Pretend the HTML comment is not there since I had to include that so the extra spaces would not get stripped out of this forum message.
Now say that you included startup.php
in another file that expects to write header information out before any HTML is sent to the client.
<?php
require('startup.php');
// write some headers here
// now write some html
The above PHP would throw an error because those extra white space characters in startup.php
were already sent to the client browser as HTML. That means we can no longer write header tags. Bummer!
So, by omitting the closing ?> in startup.php
we make sure we don't prematurely send out any HTML before we really want to.
More info on this subjuct can be found at this blog post.
Epilogue
By omitting the closing ?> tag in your PHP file you are not only doing a valid thing but a recommended thing. Good job. :D
tobiaskrause
9,160 PointsHere is a pretty good answer: http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag Google answers to everything ;) Could copy&paste it but it is not my answer i just found it
Codin - Codesmite
8,600 PointsCodin - Codesmite
8,600 PointsI upvoted as I believe this is the best answer, but from the point of view of someone that collaborates in a proffesional environment, most collabritive jobs will close PHP as a standard, a lot of PHP programmers actually see it as bad practice not to close PHP, because it encourages the use of another bad practice (whitespace at the end of your PHP). It is a misconception amongst programmers that this is the best practice (Mostly due to Zend Framework adopting it as standard) and has been widely argued for a long time.
The whitespace issue only really effects much older environments as most modern versions of PHP and apache have a buffer to handle whitespace before closing, as in some rare situations this may actually be required for example when working with UNIX console line feeds.
Also if in the rare occasion you were to echo a
tag in your PHP it will prematurely close your PHP without having a valid closing tag for your PHP.