Removing "?>"

Started by KensonPlays, November 12, 2011, 02:36:04 AM

Previous topic - Next topic

KensonPlays

I am reading a PHP, MYSQL, and JavaScript book, and I came across where it says that you can remove the "?>" if there is going to be no regular HTML after the PHP code and the author says it's good practice. Something like this:

<?php
// PHP File Contents

He says it's good practice. If it is, do you think it will work for SMF, maybe as a mod, or built in?

Owner of Mesozoic Haven

Illori

this has already been done in trunk, by sleepy.

Dean Williams

this is only recommended for noobs who stupidly put whitespace after the ?> and then cant understand errors if the code is before header output.

It's always RECOMMENDED to close your tags, in ALL languages. It provides balance and cleanliness too.

Matthew K.

I personally have never heard of it being a good practice to ignore the closing PHP tag (?>), and I don't see why it would be.

ziycon

Quote from: Labradoodle-360 on November 12, 2011, 09:13:16 AM
I personally have never heard of it being a good practice to ignore the closing PHP tag (?>), and I don't see why it would be.
+1, never heard it, I keep it the same across all languages I code in, always close tags.

MrPhil

Anyone who recommends removing ?> tags is a complete idiot. Those end tags exist for a purpose, and should be used. Maybe you can get away with it sometimes, but why do it? You're doing violence to the structure of the code, and opening up yourself to possible future problems.

feline

Quote from: MrPhil on November 12, 2011, 09:32:12 AM
Anyone who recommends removing ?> tags is a complete idiot.
Same as I think  :D .. a good editor removes white spaces on each EOL and each good programmer will write perfect code.

Andre N

It's Zend coding standard to omit the closing tag on php files containing only php
http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html#coding-standard.php-file-formatting.general
Quote
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.
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

feline

Zent is not the word of php coding ....  ;)

danielwmoore

#9
Let's take a look at what the PHP site says about it.

From http://www.php.net/manual/en/language.basic-syntax.phpmode.php
QuoteNote:

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.

and from: http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php

QuoteThe closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

As you can see, you should close the tags if you are imbedding it inside XML or XHTML (not HTML) due to the standards of XML and XHMTL.  It will still work if you don't add the closing tag, but to be compliant, you should.  However, if you are embedding XML or XHTML inside PHP, then it is not required.

It is actually helpful to leave it off in some cases, as stated at the PHP.net site, as stated in the second quote.

This is not just for "noobs" who accidentally add whitespace after the closing tag and can't figure out what's wrong, but mainly when you must require() or include(), and you are working in a group effort, it keeps your script from failing due to someone else's white space problem. 

PHP simply does not need both the opening and closing tags.  The language was designed not to need them, and it is ridiculous to try to state someone is stupid for not using them.  We do a lot of typing as programmers, and every keystroke you can save is a keystroke further away from repetitive stress injury.  (You have to love speech recognition and translation programs for normal writing, eh?) 

The ONLY reason PHP has opening and closing tags is so that it can be embedded within HTML.  It is called to "Escaping from HTML," and is one thing that makes PHP a powerful language.  When you place the final closing tag, you are causing the PHP interpreter to say, "Hey, we're escaping back to HTML from PHP, time to switch to HTML processing".  Then it tries to read the HTML, and there is NOTHING there for it.  I imagine the PHP interpreter says, "Wow... what a let down."

Hey, it's no shame if you haven't heard of this practice.  It just means you aren't hard-core into how PHP itself is coded and haven't followed the development of PHP from the inside. 

Dismissing Zend coding standard is not something to be done lightly.  These are hard-core PHP people and know what they are doing, and I can guarantee they have good reason for all their recommendations.  For someone to say that anyone following a guideline set up by them a complete idiot...  well, that's just an ignorant thing to say and shows a lack of respect for the PHP community in general.  If you are a PHP programmer with that attitude, then I certainly would never hire you for a project. 

Hey, it's not just Zend framework's standard to omit it, it is the standard of several popular and well written frameworks to do the same.  Do you really think you know more about PHP than these people who have created these framework masterpieces?  Anyone who says they do would be someone I wouldn't believe without some serious proof to back it up. 

Matthew K.

It may be suitable for some cases, but even though, those scenarios I could argue with.

However, I do believe outside of those narrow scenarios that are possible, it's a sloppy practice to not close the file with ?>.

KensonPlays

Quote from: danielwmoore on November 12, 2011, 06:15:40 PM
Let's take a look at what the PHP site says about it.

From http://www.php.net/manual/en/language.basic-syntax.phpmode.php
QuoteNote:

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.

and from: http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php

QuoteThe closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

As you can see, you should close the tags if you are imbedding it inside XML or XHTML (not HTML) due to the standards of XML and XHMTL.  It will still work if you don't add the closing tag, but to be compliant, you should.  However, if you are embedding XML or XHTML inside PHP, then it is not required.

It is actually helpful to leave it off in some cases, as stated at the PHP.net site, as stated in the second quote.

This is not just for "noobs" who accidentally add whitespace after the closing tag and can't figure out what's wrong, but mainly when you must require() or include(), and you are working in a group effort, it keeps your script from failing due to someone else's white space problem. 

PHP simply does not need both the opening and closing tags.  The language was designed not to need them, and it is ridiculous to try to state someone is stupid for not using them.  We do a lot of typing as programmers, and every keystroke you can save is a keystroke further away from repetitive stress injury.  (You have to love speech recognition and translation programs for normal writing, eh?) 

The ONLY reason PHP has opening and closing tags is so that it can be embedded within HTML.  It is called to "Escaping from HTML," and is one thing that makes PHP a powerful language.  When you place the final closing tag, you are causing the PHP interpreter to say, "Hey, we're escaping back to HTML from PHP, time to switch to HTML processing".  Then it tries to read the HTML, and there is NOTHING there for it.  I imagine the PHP interpreter says, "Wow... what a let down."

Hey, it's no shame if you haven't heard of this practice.  It just means you aren't hard-core into how PHP itself is coded and haven't followed the development of PHP from the inside. 

Dismissing Zend coding standard is not something to be done lightly.  These are hard-core PHP people and know what they are doing, and I can guarantee they have good reason for all their recommendations.  For someone to say that anyone following a guideline set up by them a complete idiot...  well, that's just an ignorant thing to say and shows a lack of respect for the PHP community in general.  If you are a PHP programmer with that attitude, then I certainly would never hire you for a project. 

Hey, it's not just Zend framework's standard to omit it, it is the standard of several popular and well written frameworks to do the same.  Do you really think you know more about PHP than these people who have created these framework masterpieces?  Anyone who says they do would be someone I wouldn't believe without some serious proof to back it up. 
Nice post daniel. I'm still LEARNING PHP so I'm debating whether to keep it in or take it out. I'm most likely not going to put HTML after PHP. I prefer so far to echo out HTML in PHP.

Owner of Mesozoic Haven

danielwmoore

Quote from: Labradoodle-360 on November 12, 2011, 07:27:26 PM
It may be suitable for some cases, but even though, those scenarios I could argue with.

However, I do believe outside of those narrow scenarios that are possible, it's a sloppy practice to not close the file with ?>.

While it is considered sloppy not to close a .html file with </html> (even though it still works), it is not considered sloppy to not use the ?> at the end of a PHP file.  Trust me, if so many top level PHP experts are advising that programmers leave it out, it is not a sloppy practice. 

What I do in my PHP files is something that the CodeIgniter framework encourages.  Instead of the closing "?>" at the end of the file, I use the following:

/* End of file filename.php */
/* Location: ./relative/path/to/file/from/primary/application/filename.php */

In this way, those who "feel the need" to close, have that need satisfied.  It doesn't hurt anything to have it there, and helps provide structure for those that are confused by the lack of closing "?>".   The "Location" part of it makes it easy to share updates to a large project with coworkers if you aren't using SVN, as they will know where to place the file within the project.   It's not at all necessary, though. 

MrPhil

So, omitting the ?> is for sloppy editors who somehow can't avoid leaving whitespace after it. Unnecessary if you can use an editor properly.

Matthew K.

Well, there are apparent uses for not closing it, using require/include[_once], but beyond that I think it's just...sloppy not to use it, if you don't HAVE to drop it.

Illori

*note* if you wish to mark this unsolved please explain why, as it is not a support issue.

danielwmoore

No, not sloppy to not use it.  PHP was designed not to use it.  PHP was designed to allow escaping out of scripting and back into HTML.  There is absolutely no value in escaping into HTML if the script is closing.  To me, what is truly sloppy, is to escape back into HTML if you are not entering into HTML.  It is including extra code into a program that is not necessary.  As I was taught at the University, you should always minimize the number of instructions necessary for a program to function properly.  Extraneous instructions are bad, sloppy practice, and extraneous instructions run wild lead quickly to bloat that slows down software.

The "need to close" this final tag is from people who learned XML and HTML and then learned a programming language (PHP) as an afterthought to do more on the web.  I, on the other hand, began programming in 1972 with punch cards and a main frame that had limited memory, so you had to program efficiently.  I was 8 years old.  My father worked for IBM, and I was in a particularly good position to learn young, which was uncommon in that day. 

As I stated before.  If you are calling the recommended practice of the actual experts in the field "sloppy", then I question your professionalism and would definitely not hire you for a project.


Norv

#17
I don't think SMF must use the PHP ?> closing tags anymore.

Part of the reason is that PHP is designed with some features behaving differently from what common practice is in other languages (not exactly a surprise... and if one, not always a pleasant one). As stated above, PHP documentation itself declares them optional and does not recommend closing tags in all usage scenarios (they're needed only sometimes), because their purpose is not only the obvious common-sense "start tag; do stuff; close tag" as in other languages, not only mark the end of PHP, but mark the beginning of the output for anything added or left to it.
I must have seen the problems with a poor space or newline or other characters at the end of the files in SMF causing troubles in these support boards for ages (in particular for visual verification, but not only iirc), and it's actually hard to trace to the source if you don't realize what is causing it in the first place. While I sympathize with the wish to keep the practices across languages, it simply isn't possible: it's arguable for example that practices in other languages may state that you'd better end your source files with a newline too, which simply is the kind of practice that is causing the troubles with PHP.

Yes, quite a number of PHP frameworks (CodeIgniter, Zend, but not only) have given it up, and even disallow it. It's not random, and it's not without grounds. Per the language specification itself, they're optional, even slightly recommended to leave out when include()-ing/require()-ing, and these codebases are not using those scenarios where you need them.
To-do lists are for deferral. The more things you write down the later they're done... until you have 100s of lists of things you don't do.

File a security report | Developers' Blog | Bug Tracker


Also known as Norv on D* | Norv N. on G+ | Norv on Github

Norv

Illori: I'd keep this topic unsolved, to discuss the matter throughoutfully if people wish to do so. Actually, I'll move it to features discussions boards, even if it's not exactly that.
To-do lists are for deferral. The more things you write down the later they're done... until you have 100s of lists of things you don't do.

File a security report | Developers' Blog | Bug Tracker


Also known as Norv on D* | Norv N. on G+ | Norv on Github

KensonPlays

So, Daniel, what you are saying is that you should only include the "?>" when you are adding HTML at the end? Otherwise keep the "?>" out? I think that it's good practice because you're less likely to have errors (like I've experienced with some mods in SMF; where when it's installed, I'll see a string of characters at the very top of the page before SMF kicks in, and looks ugly).

Owner of Mesozoic Haven

Advertisement: