407

I keep reading it is poor practice to use the PHP close tag ?> at the end of the file. The header problem seems irrelevant in the following context (and this is the only good argument so far):

Modern versions of PHP set the output_buffering flag in php.ini If output buffering is enabled, you can set HTTP headers and cookies after outputting HTML because the returned code is not sent to the browser immediately.

Every good practice book and wiki starts with this 'rule' but nobody offers good reasons. Is there another good reason to skip the ending PHP tag?

Mehran
  • 282
  • 1
  • 6
  • 17
johnlemon
  • 20,761
  • 42
  • 119
  • 178
  • 3
    possible duplicate of [why in some scripts they omit the closing php tag ?> ](http://stackoverflow.com/questions/3219383/why-in-some-scripts-they-omit-the-closing-php-tag) – Gordon Dec 10 '10 at 16:03
  • @Christian - You mean that using the output_buffering is lazy, or leaving off the `?>` is lazy? – El Yobo Dec 12 '10 at 12:24
  • 4
    @Gordon - I don't think it's a dup, the OP knows the ostensible reasons, just wants to know if it is completely resolved with output buffering. – El Yobo Dec 12 '10 at 12:27
  • http://php.net/manual/en/language.basic-syntax.phptags.php :) – abuduba Dec 05 '12 at 19:55
  • Real reason: the closing tag is so 2004. – tacone Jul 30 '14 at 12:12
  • *"The header problem seems irrelevant"* is a narrow-minded way of looking at this. Just because the default `output_buffering` means that accidentally outputting whitespace probably won't stop you from setting headers, that doesn't mean that accidentally outputting whitespace at the beginning of your response is not a bug. What if your response is `text/plain`, for example, and going to be rendered literally to the user? JSON and HTML are forgiving of leading whitespace, but not everything you might spit out from a webserver is. – Mark Amery Nov 28 '14 at 17:46
  • 6
    A better question would be: Why would one include the close tag? Code is evil. The best code is no code at all. If a problem can be eliminated instead of solved with code, this is better than having code. In this case, there is no problem needing to be solved. The code works fine without the close tag. – still_dreaming_1 Feb 14 '15 at 16:44
  • 20
    Oh god, this isn't the place for the tabs vs spaces holy war, lol :) – Kevin Wheeler Aug 14 '15 at 03:55

14 Answers14

342

Sending headers earlier than the normal course may have far reaching consequences. Below are just a few of them that happened to come to my mind at the moment:

  1. While current PHP releases may have output buffering on, the actual production servers you will be deploying your code on are far more important than any development or testing machines. And they do not always tend to follow latest PHP trends immediately.

  2. You may have headaches over inexplicable functionality loss. Say, you are implementing some kind payment gateway, and redirect user to a specific URL after successful confirmation by the payment processor. If some kind of PHP error, even a warning, or an excess line ending happens, the payment may remain unprocessed and the user may still seem unbilled. This is also one of the reasons why needless redirection is evil and if redirection is to be used, it must be used with caution.

  3. You may get "Page loading canceled" type of errors in Internet Explorer, even in the most recent versions. This is because an AJAX response/json include contains something that it shouldn't contain, because of the excess line endings in some PHP files, just as I've encountered a few days ago.

  4. If you have some file downloads in your app, they can break too, because of this. And you may not notice it, even after years, since the specific breaking habit of a download depends on the server, the browser, the type and content of the file (and possibly some other factors I don't want to bore you with).

  5. Finally, many PHP frameworks including Symfony, Zend and Laravel (there is no mention of this in the coding guidelines but it follows the suit) and the PSR-2 standard (item 2.2) require omission of the closing tag. PHP manual itself (1,2), Wordpress, Drupal and many other PHP software I guess, advise to do so. If you simply make a habit of following the standard (and setup PHP-CS-Fixer for your code) you can forget the issue. Otherwise you will always need to keep the issue in your mind.

Bonus: a few gotchas (actually currently one) related to these 2 characters:

  1. Even some well-known libraries may contain excess line endings after ?>. An example is Smarty, even the most recent versions of both 2.* and 3.* branch have this. So, as always, watch for third party code. Bonus in bonus: A regex for deleting needless PHP endings: replace (\s*\?>\s*)$ with empty text in all files that contain PHP code.
isherwood
  • 58,414
  • 16
  • 114
  • 157
Halil Özgür
  • 15,731
  • 6
  • 49
  • 56
  • Slightly different regex I used to find the tags with Netbeans IDE, for Find & Replace dialog: `\?>(?s:.){0,10}\Z` Short explanation: http://changeset.hr/blog/miscellaneous/catch-near-eof-with-regex – frnhr Jan 21 '13 at 01:20
  • @Cek, it catches any text -including code- that is 10 characters or less after `?>`: e.g. it matches -and would delete- `?> Hello` in ` Hello`. We'd want to clear only close tags. – Halil Özgür Jan 21 '13 at 07:40
  • @HalilÖzgür true, that's why it's a *search*, not a *search&replace* regex :) – frnhr Jan 22 '13 at 00:06
  • 6
    Worse, apache 2.4.6 with PHP 5.4 actually seg faults on our production machines when there's empty space behind the closing tag. I just wasted hours until I finally narrowed down the bug with strace. Here is the error that apache throws: `[core:notice] [pid 7842] AH00052: child pid 10218 exit signal Segmentation fault (11)`. – Artem Russakovskii Jun 09 '14 at 05:39
  • Most of this answer is lacking context. Only a small amount of it is provided. It makes it difficult to understand what these points have to do with the closing PHP tag? I am not suggesting the are invalid points, just that the lack of more contextual explanation makes it difficult to understand. It seems like it needs to start out with some technical explanation that helps the points to make more sense. – still_dreaming_1 Feb 14 '15 at 16:33
  • @INTPnerd, could you provide some examples where this lacks context/technical explanation and how can it be improved? I'd really like to improve it, thanks for your input. – Halil Özgür Feb 14 '15 at 21:32
  • It seems my brain was not working before, it seems to be working now. Anyway it seems you did preface the points with the idea that sending headers earlier than the normal course could be bad. So you seem to imply headers could get sent as a result of using the php closing tag. I just wish you had explained more about how the 2 are related. I have a pretty good guess, but I would like more details. Or perhaps the problem is that I don't really understand headers. What types of headers are you referring to? HTTP headers, HTML headers? – still_dreaming_1 Feb 15 '15 at 00:25
  • 3
    @INTPnerd Oh, the question and most of the answers refer to the header thing, so I assumed anyone reading this thread would have an idea about it. The underlying issue and the actual cause of the problems in many of the answers here is unneeded whitespace after `?>`, which (just like any output) causes the headers to be sent as soon as it's output. There are no "HTML headers" (other than the unrelated HTML5 `
    ` tag).
    – Halil Özgür Feb 15 '15 at 12:13
  • 2
    This should work, regardless of global modifier: \s*\?>\s*\Z Notice the '\Z' at the end. It ensures that you're capturing a php closing tag if and only if it's the last non-whitespace at the very end of the file. – Erutan409 Jul 31 '15 at 15:14
  • @Erutan409 definitely, if that's allowed in your editor/tool of choice. – Halil Özgür Jul 31 '15 at 21:52
127

The reason you should leave off the php closing tag (?>) is so that the programmer doesn't accidentally send extra newline chars.

The reason you shouldn't leave off the php closing tag is because it causes an imbalance in the php tags and any programmer with half a mind can remember to not add extra white-space.

So for your question:

Is there another good reason to skip the ending php tag?

No, there isn't another good reason to skip the ending php tags.

I will finish with some arguments for not bothering with the closing tag:

  1. People are always able to make mistakes, no matter how smart they are. Adhering to a practice that reduces the number of possible mistakes is (IMHO) a good idea.

  2. PHP is not XML. PHP doesn't need to adhere to XMLs strict standards to be well written and functional. If a missing closing tag annoys you, you're allowed to use a closing tag, it's not a set-in-stone rule one way or the other.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 3
    > _any programmer with half a mind can remember to not add extra white-space._ Even better, any developer with 1/2 a mind can add a pre-commit hook to SVC so that any trailing spaces are automatically deleted: no fuss, no muss. – BryanH Dec 11 '12 at 22:24
  • 6
    @BryanH, until you have a file where trailing whitespace is absolutely required, but that's *very* rare. – zzzzBov Jan 03 '13 at 14:14
  • 1
    You're right, of course. Check out [mario's answer](http://stackoverflow.com/a/4465825/41688) for some very cool alternatives. – BryanH Jan 03 '13 at 21:46
  • >"The reason you shouldn't leave off the php closing tag is because it causes an imbalance in the php tags and any programmer with half a mind can remember to not add extra white-space." On Windows, maybe. On UNIX-like systems, all files end with \n and programs add it for you. **EDIT:** Aha! As noted below by @mario, PHP eats that, actually. – Andrea Oct 17 '14 at 19:51
  • Even better, any programmer with half of half of half of a mind can neglect to include the closing PHP tag. Also, the really good developers try hard not to mix PHP and other code in the same file, so both the starting and closing PHP tags are redundant, except that PHP is so stupid as to require the opening PHP tag. It should only be required if you use the closing tag, and then need to start PHP code again. – still_dreaming_1 Feb 14 '15 at 16:42
  • 2
    Even more significant is that even programmers with double of a triple of a mind is human and forgets things somethings. – Sebastian Mach Jan 11 '16 at 14:25
63

It's a newbie coding style recommendation, well-intentioned, and advised by the manual.

  • Eschewing ?> however solves just a trickle of the common headers already sent causes (raw output, BOM, notices, etc.) and their follow-up problems.

  • PHP actually contains some magic to eat up single linebreaks after the ?> closing token. Albeit that has historic issues, and leaves newcomers still susceptible to flaky editors and unawarely shuffling in other whitespace after ?>.

  • Stylistically some developers prefer to view <?php and ?> as SGML tags / XML processing instructions, implying the balance consistency of a trailing close token. (Which btw, is useful for dependency-conjoining class includes to supplant inefficient file-by-file autoloading.)

  • Somewhat uncommonly the opening <?php is characterized as PHPs shebang (and fully feasible per binfmt_misc), thereby validating the redundancy of a corresponding close tag.

  • There's an obvious advise discrepancy between classic PHP syntax guides mandating ?>\n and the more recent ones (PSR-2) agreeing on omission.
    (For the record: Zend Framework postulating one over the other does not imply its inherent superiority. It's a misconception that experts were drawn to / target audience of unwieldy APIs).

  • SCMs and modern IDEs provide builtin solutions mostly alleviating close tag caretaking.

Discouraging any use of the ?> close tag merely delays explaining basic PHP processing behaviour and language semantics to eschew infrequent issues. It is practical still for collaborative software development due to proficiency variations in participants.

Close tag variations

  • The regular ?> close tag is also known as T_CLOSE_TAG, or thus "close token".

  • It comprises a few more incarnations, because of PHPs magic newline eating:

    ?>\n (Unix linefeed)

    ?>\r (Carriage return, classic MACs)

    ?>\r\n (CR/LF, on DOS/Win)

    PHP doesn't support the Unicode combo linebreak NEL (U+0085) however.

    Early PHP versions had IIRC compile-ins limiting platform-agnosticism somewhat (FI even just used > as close marker), which is the likely historic origin of the close-tag-avoidance.

  • Often overlooked, but until PHP7 removes them, the regular <?php opening token can be validly paired with the rarely used </script> as odd closing token.

  • The "hard close tag" isn't even one -- just made that term up for analogy. Conceptionally and usage-wise __halt_compiler should however be recognized as close token.

    __HALT_COMPILER();
    ?>
    

    Which basically has the tokenizer discard any code or plain HTML sections thereafter. In particular PHAR stubs make use of that, or its redundant combination with ?> as depicted.

  • Likewise does a void return; infrequently substitute in include scripts, rendering any ?> with trailing whitespace noneffective.

  • Then there are all kinds of soft / faux close tag variations; lesser known and seldomly used, but usually per commented-out tokens:

    • Simple spacing // ? > to evade detection by PHPs tokenizer.

    • Or fancy Unicode substitutes // ﹖﹥ (U+FE56 SMALL QUESTION MARK, U+FE65 SMALL ANGLE BRACKET) which a regexp can grasp.

    Both mean nothing to PHP, but can have practical uses for PHP-unaware or semi-aware external toolkits. Again cat-joined scripts come to mind, with resulting // ? > <?php concatenations that inline-retain the former file sectioning.

So there are context-dependent but practical alternatives to an imperative close tag omission.

Manual babysitting of ?> close tags is not very contemporary either way. There always have been automation tools for that (even if just sed/awk or regex-oneliners). In particular:

phptags tag tidier

https://fossil.include-once.org/phptags/

Which could generally be used to --unclose php tags for third-party code, or rather just fix any (and all) actual whitespace/BOM issues:

  • phptags --warn --whitespace *.php

It also handles --long tag conversion etc. for runtime/configuration compatibility.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • 8
    Yes, blunt AND provocative phrasing attract downvotes. From what I've read over time, leaving out closing token has absolutely no downsides as long as you don't work with software that can't handle that. Unless you can actually prove that omitting closing tokens is bad and a very noob thing to do, my downvote stays ;) – jpeltoniemi Jul 07 '12 at 16:55
  • 2
    @Pichan: I'll allow it. But I presume you haven't quite understood what's being told here. Eschewing and avoiding are two different things. And problems half solved are the result of snake oil advise. – mario Jul 07 '12 at 17:02
  • @mario I kind of see your point. Still, I've seen both newbies and pros omit the token and probably the only ones who have pointed out the missing token as a mistake have been of the noob group. Your answer is great for newbies who havent heard of this and mentioning "newbies use this" might really steer them away since who wants to be a noob. That said, I've yet to see any actual reason to not omit the ending token if you know what you're doing. Personally I omit it for aesthetics and habit from working with other people who aren't so pedantic about their file endings. – jpeltoniemi May 03 '13 at 08:26
  • 6
    @mario: **It's a newbie coding style recommendation**. I disagree. The entire Zend Framework omits close tags. I think that's a very personal choice, I really prefer to leave my – Daniele Vrut Oct 18 '13 at 08:55
  • 1
    What about HTML? Is it then needed? For exammple `Hello World` ??? I am genuinely curious as it isn't specifically mentioned. – WASasquatch Jan 20 '15 at 06:54
  • 1
    @WASasquatch Yes. If you're switching between HTML and PHP mode, then you'll need close tokens in any case. (Not really relevant to the original question here.) – mario Jan 20 '15 at 06:56
25

It isn't a tag…

But if you have it, you risk having white space after it.

If you then use it as an include at the top of a document, you could end up inserting white space (i.e. content) before you attempt to send HTTP headers … which isn't allowed.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 10
    If it's not a tag what is it ? – johnlemon Dec 10 '10 at 16:46
  • Can you please provide an example ? Maybe my php config is whack , but I can't reproduce the problem. – johnlemon Dec 10 '10 at 16:47
  • @danip -- file1.php: `[space][space][space]` -- file2.php: `` – Matt Huggins Dec 10 '10 at 17:01
  • 2
    file1.php: ` ` then file2.php: `` – Quentin Dec 10 '10 at 17:13
  • I believe the header example is an old php problem solved now by the php config. – johnlemon Dec 10 '10 at 17:15
  • 1
    There are downsides to output buffering as well; it uses more memory on the server (as all output has to be stored in RAM until it's output; without buffering it just goes straight away). It's also ever so slightly slower. Neither of these will be issues in most cases, but I'm lazy anyway, so why not just leave the closing tag off? I use `phpcs` to ensure that the closing tag is left of every single one of my files and then I don't worry about output buffering :) – El Yobo Dec 12 '10 at 12:26
  • Also, you still will have the extra white space, even if output buffering is enabled; it just won't screw up your headers as well. – El Yobo Dec 12 '10 at 12:29
  • El Yobo, output buffering is still required for stuff like compression. It also allows the server to actually write a content-length header for dynamic content. – Christian Dec 13 '10 at 14:44
  • 1
    @danip: If you have set the output_buffer flag in the php configuration file, you could not reproduce this problem. – Jichao Mar 14 '12 at 02:31
  • You may feel this absurdly pedantic, but -1 for *"it isn't a tag"*. It's not clear what you mean by this, and PHP doesn't agree with you - [they call it the `T_CLOSE_TAG`](http://php.net/tokens). – Mark Amery Nov 28 '14 at 17:54
  • This is the only answer that actually clearly explains the problem – Chuck Le Butt Jan 04 '21 at 17:21
18

According to the docs, it's preferable to omit the closing tag if it's at the end of the file for the following reason:

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

PHP Manual > Language Reference > Basic syntax > PHP tags

Prags
  • 2,457
  • 2
  • 21
  • 38
Asaph
  • 159,146
  • 25
  • 197
  • 199
16

It's pretty useful not to let the closing ?> in.

The file stays valid to PHP (not a syntax error) and as @David Dorward said it allows to avoid having white space / break-line (anything that can send a header to the browser) after the ?>.

For example,

<?
    header("Content-type: image/png");
    $img = imagecreatetruecolor ( 10, 10);
    imagepng ( $img);
?>
[space here]
[break line here]

won't be valid.

But

<?
    header("Content-type: image/png");
    $img = imagecreatetruecolor ( 10, 10 );
    imagepng ( $img );

will.

For once, you must be lazy to be secure.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shikiryu
  • 10,180
  • 8
  • 49
  • 75
  • 3
    That's why I put secure in italic. It prevents you from errors that may cost you a lot of time for a unwanted space after the `?>`. _secure_ may not be the good word here. Anyway, it doesn't **fix** anything (it's not bad code to me to prevent things, an `echo` here would have been a bad code) **but/and** it's still valid. Prove me wrong on this. – Shikiryu Dec 13 '10 at 14:44
  • I didn't downvote you, btw. But my point is compatible with yours. It doesn't fix anything. I never said it's not valid, so I don't need to prove anything. – Christian Dec 17 '10 at 16:17
  • 1
    Chouchenos, I bet you meant safe, rather than secure. IMHO a neg for that was too much. Brought you back to square one. ;-) You weren't saying the wrong thing after all. Even PHP development guidelines encourage you to do that. It's in fact much better to rely on closing tag omission, rather than output buffering, for example. The latter would be like setting display_errors to off. Pure Cheating. And a good chance that your app wouldn't be portable. I really think that, as a rule, it's better not to rely on output buffering at all. – maraspin Dec 17 '10 at 20:01
  • 1
    I don't want to question people that like putting `?>` at the end of a php only file. but have you ever had the need to debug some error caused by trailing spaces? Also not all servers are configured the same way especially when you move to another host, catching that errors is very time consuming. If you want put `?>` just do it, if you also add some trailing space and your team need to debug that be ready to blames on git ^^ – CoffeDeveloper Feb 25 '15 at 21:15
10

Well, I know the reason, but I can't show it:

For files that contain only PHP code, the closing tag (?>) is never permitted. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.

Source: http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tawfekov
  • 5,084
  • 3
  • 28
  • 51
  • 24
    That tag being "never permitted" is probably a Zend coding standard, but it's not a syntax rule for PHP file formatting. – Matt Huggins Dec 10 '10 at 17:02
9

Well, there are two ways of looking at it.

  1. PHP code is nothing more than a set of XML processing instructions, and therefore any file with a .php extension is nothing more than an XML file that just so happens to be parsed for PHP code.
  2. PHP just so happens to share the XML processing instruction format for its open and close tags. Based on that, files with .php extensions MAY be valid XML files, but they don't need to be.

If you believe the first route, then all PHP files require closing end tags. To omit them will create an invalid XML file. Then again, without having an opening <?xml version="1.0" charset="latin-1" ?> declaration, you won't have a valid XML file anyway... So it's not a major issue...

If you believe the second route, that opens the door for two types of .php files:

  • Files that contain only code (library files for example)
  • Files that contain native XML and also code (template files for example)

Based on that, code-only files are OK to end without a closing ?> tag. But the XML-code files are not OK to end without a closing ?> since it would invalidate the XML.

But I know what you're thinking. You're thinking what does it matter, you're never going to render a PHP file directly, so who cares if it's valid XML. Well, it does matter if you're designing a template. If it's valid XML/HTML, a normal browser will simply not display the PHP code (it's treated like a comment). So you can mock out the template without needing to run the PHP code within...

I'm not saying this is important. It's just a view that I don't see expressed too often, so what better place to share it...

Personally, I do not close tags in library files, but do in template files... I think it's a personal preference (and coding guideline) based more than anything hard...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • 1
    This is completely false. PHP does NOT translate those tags into XML, and thus no imbalance is being generated. – Drachenkatze Oct 22 '13 at 16:00
  • 7
    @Felicitus: I guess you missed the part that says *"I'm not saying this is important. It's just a view that I don't see expressed too often, so what better place to share it..."* It's not about PHP translating tags into XML. It's about what happens when a file is interpreted in an XML context (like HTML, or editors, etc)... But point missed... – ircmaxell Oct 22 '13 at 16:04
  • The only resemblance between PHP and XML are angular brackets. This answer feels more misleading than useful IMHO. – Niki Romagnoli Mar 22 '21 at 11:46
7

In addition to everything that's been said already, I'm going to throw in another reason that was a huge pain for us to debug.

Apache 2.4.6 with PHP 5.4 actually segmentation faults on our production machines when there's empty space behind the closing php tag. I just wasted hours until I finally narrowed down the bug with strace.

Here is the error that Apache throws:

[core:notice] [pid 7842] AH00052: child pid 10218 exit signal Segmentation fault (11)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
6

"Is there another good reason (other than the header problem) to skip the ending php tag?"

You don't want to inadvertently output extraneous whitepace characters when generating binary output, CSV data, or other non-HTML output.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1238364
  • 71
  • 1
  • 2
  • 1
    I've had a client complain because their XML parser was refusing our output when it had an extra blank line at the beginning. Worse, it was only happening on the one server out of seven with an extra line after the closing tag in an unrevisioned configuration file. – eswald Jul 03 '12 at 17:03
5

Pros

Cons

  • Avoids headache with adding inadvertently whitespaces after the closing tag, because it breaks the header() function behavior... Some editors or FTP clients / servers are also known to change automatically the end of files (at least, it's their default configuration)
  • PHP manual says closing tag is optional, and Zend even forbids it.

Conclusion

I would say that the arguments in favor of omitting the tag look stronger (helps to avoid big headache with header() + it's PHP/Zend "recommendation"). I admit that this isn't the most "beautiful" solution I've ever seen in terms of syntax consistency, but what could be better ?

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
2

As my question was marked as duplicate of this one, I think it's O.K. to post why NOT omitting closing tag ?> can be for some reasons desired.

  • With complete Processing Instructions Syntax (<?php ... ?>) PHP source is valid SGML document, which can be parsed and processed without problems with SGML parser. With additional restrictions it can be valid XML/XHTML as well.

Nothing prevents you from writing valid XML/HTML/SGML code. PHP documentation is aware of this. Excerpt:

Note: Also note that if you are embedding PHP within XML or XHTML you will need to use the < ?php ?> tags to remain compliant with standards.

Of course PHP syntax is not strict SGML/XML/HTML and you create a document, which is not SGML/XML/HTML, just like you can turn HTML into XHTML to be XML compliant or not.

  • At some point you may want to concatenate sources. This will be not as easy as simply doing cat source1.php source2.php if you have inconsistency introduced by omitting closing ?> tags.

  • Without ?> it's harder to tell if document was left in PHP escape mode or PHP ignore mode (PI tag <?php may have been opened or not). Life is easier if you consistently leave your documents in PHP ignore mode. It's just like work with well formatted HTML documents compared to documents with unclosed, badly nested tags etc.

  • It seems that some editors like Dreamweaver may have problems with PI left open [1].

mip
  • 8,355
  • 6
  • 53
  • 72
0

There are 2 possible use of php code:

  1. PHP code such as class definition or function definition
  2. Use PHP as a template language (i.e. in views)

in case 1. the closing tag is totally unusefull, also I would like to see just 1 (one) php open tag and NO (zero) closing tag in such a case. This is a good practice as it make code clean and separate logic from presentation. For presentation case (2.) some found it is natural to close all tags (even the PHP-processed ones), that leads to confution, as the PHP has in fact 2 separate use case, that should not be mixed: logic/calculus and presentation

Daniele Cruciani
  • 623
  • 1
  • 8
  • 15
0

If I understand the question correctly, it has to do with output buffering and the affect this might have on closing/ending tags. I am not sure that is an entirely valid question. The problem is that the output buffer does not mean all content is held in memory before sending it out to the client. It means some of the content is.

The programmer can purposely flush the buffer, or the output buffer so does the output buffer option in PHP really change how the closing tag affects coding? I would argue that it does not.

And maybe that is why most of the answers went back to personal style and syntax.

Ruz
  • 246
  • 1
  • 3