MyBB Community Forums

Full Version: You Close Your PHP Code Tags? (?>)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I'm a PHP beginner and I try to learn how some developers create their applications. I've seen scripts with unclosed PHP tag (?>) and I try to understand why!?

<?php
// script content
?>

From what I read on the internet, this practice is recommended (including by Zend Framework) when using include() or require(), to prevents accidental whitespace or new lines being added after the PHP closing tag.
I'm not sure if and how useful is this practice. Personally, it makes me feel a little... wrong or insecure. So... I would like to know the opinion of developers about how good is the practice to not close php tags (of course, for PHP-files only)?

You ever use it? Should be avoided? Or ... in which case it is really a good practice?

Thank you.
It's recommended best practice, though I don't usually do it. For some strange reason I always type out opening and closing tags first, with three lines between them, and then start to code.

The whole deal is that if you include the ?> tag with whitespace at the end, headers can't be sent or something resulting in PHP Errors being output. I've not encountered this problem personally, but hey, YMMV.
Thank you, Seabody. OK. So... it's related to... output header?
I found information related to "output_buffering" flag in "php.ini" but I still don't understand. For example, if this is enabled, is recommended to use unclosed PHP tag method?

L.E.: I mean... in which case it is really a good practice?
Output buffering means that anything that is echo'd out will be stored in the buffer until it is explicitly output (or the script terminates). Take for instance:

<?php

echo "Hello World!";
header("Content type: text/html");

?>

This fails as output has been sent before a header. However this:

<?php

ob_start(); // starts output buffering

echo "Hello World!";
header("Content type: text/html");

?>

This works as ob_start() has been called. I'm working on a couple of projects ATM, and I'm using output buffering as stuff is liable to be output before I've sent all the headers I need to. I force the output to be output using something like this:

<?php

function output()
{
$contents = ob_get_contents();
echo $contents;
ob_end_clean();
ob_start();
}

?>

(I can't remember my exact code ATM)

Edit: I'm not entirely sure on the technicalities of output buffering and closing tags. Do some googling or hope a more experienced coder comes around.
(2013-11-15, 04:44 AM)Seabody Wrote: [ -> ]Output buffering means that anything that is echo'd out will be stored in the buffer until it is explicitly output (or the script terminates).
Good and simple explanation. Thank's.

(2013-11-15, 04:44 AM)Seabody Wrote: [ -> ]Edit: I'm not entirely sure on the technicalities of output buffering and closing tags. Do some googling or hope a more experienced coder comes around.
Yeap... never mind. Probably, I will not ever use this method anyway. I just wanted to understand WHEN it is really useful to use this practice.
Google is a good friend, I know Toungue but there are a lot of opinions, pro and con (including those who say it is a practice for lazy coders Big Grin).

Thank you for clarification related to "output buffering".
L.E.: I wanted to give you +1 rep, but "You have already given as many reputation ratings as you are allowed to for today." so... I will do it tomorow. Done.
As per standard coding practice of Zend framework also, they say to do such.

If a PHP file cintains only PHP codes it is recommended not to close the PHP tag (?>). They say PHP doesn't require it and its a standard good practice in order to avoid accidental injection of white space and the all too common “headers already sent” error.

http://framework.zend.com/manual/1.12/en...ng.general

I've never done this. Just make sure you have no blank spaces after the ?> and you'll be fine. I generally close all the tags (and braces) as soon as I open it. Whatever the tag or language it is.
I never close them unless I'm doing something as backwards as mixing PHP and HTML in a file (such as when working with Wordpress templates). I terminate with a single blank line and that's that.
I hav read the information in why not to terminate php tags but I cannot brig myself to leave them off. Call it compulsive, but I feel much better with them closed.