This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 7 years ago.
I have been working with Wordpress and other CMS and I have noticed that some of the php files do not have a closing ?>.
When I write my own files, I have to include the ?>, otherwise the software crashes. I'm wondering how they make it work, or if there are specific sections of a theme that accept this type of php files. Maybe the closing bracket ?> is embedded in a different file that I haven't seen.
Does anyone has an explanation for this?
If I decide to put the ?> on these files, will the system crash? or I can safely write them with the closing bracket?
From the official PHP documentation:
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.
http://php.net/manual/en/language.basic-syntax.phptags.php
Related
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 8 years ago.
In some scripts I see that they omit writing a closing tag ?> for the script. Why is it and should I do this as well?
(I'm sure they have not forgotten it.)
Well, omitting the closing tag is just one solution for avoiding blanks and other characters at the end of file. For example any char which is accidentally added behind the closing tag would trigger an error when trying to modify header info later.
Removing the closing tag is kind of "good practice" referring to many coding guidelines.
From PHP: Instruction Separation
The 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.
php.net on PHP tags:
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.
They do it to avoid risking to have whitespaces after the closing tag which may stop headers to work.
This is, of course, true for PHP-only files.
CodeIgniter Framework suggests to omit closing tags for
"... can cause unwanted output, PHP errors or blank pages".
You can read it here.
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.
Are the examples still valid in this context?
It shows unwanted white space / blank page. HTTP headers do not work for those unwanted whitespace.
Most JavaScript injection is made at the end of the file. It will show an error message and breaks the code, injected JavaScript code does not get executed.
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 5 years ago.
I have recently seen code samples of php which looks something like this:
<?php
//some code here
//some more code
Is it possible to write a php code without closing tags?
thanks in advance.
This is from PHP Manual
"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."
Yeah. In fact it's recommended to do that. IDE softwares like, PhpStorm, for example, are marking it as warning recommending to you to remove the PHP closing tag.
Explanation:
It's unnecessary.
It's easy to accidentally add significant whitespace after the ?>, which will be output to the client, which in turn can lead to obscure 'headers already sent' errors (this happens when an included file contains whitespace, and you try to set a header after including that file).
I think this question was answered before here:
Why would one omit the close tag?
Why do some scripts omit the closing PHP tag, '?>'?
PHP end tag "?>"
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 7 years ago.
Looking into code of few sites I noted that some php files have ?> at the end of file and some doesn't. Doesn't it matter - and if it does, when must I put ?> at the end of the file and when mustn't I?
Never use ?> at the end of the file.
It's entirely optional but including it provides the opportunity to slip whitespace into the output by accident. If you do that in a file that you include or require before you try to output headers then you'll break your code.
Putting ?> at the end of a PHP file has only drawbacks.
Putting ?> is optional if it is the last PHP tag. Omitting it in the last tag will benefit you in one case. There may be case where text editor append hidden special character after that tag. This could create problem. Omitting last closing tag will solve this problem.
From PHP Official Documentation - Instruction separation
The 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.
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 8 years ago.
I tried to write a simple php file like this (and it worked):
<?php
echo("OK");
my question is:
Is it safe to write a php file without the "?>" at the end ?
Yes, it's documented that the closing tag at the end is optional.
In fact, it's better if you don't use it because if you do then sometimes extra whitespace at the end of your source ends up in the output when you don't want it to.
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.
It is recommended not to use end tag when PHP file contains only PHP code.
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.
http://php.net/manual/en/language.basic-syntax.phptags.php
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 8 years ago.
While I was developing with Magento, I found out that I don't need to put php end tag (?>) if I don't use HTML below PHP code. Is it safe and why don't we just put end tag?? Is it useful??
The official stance:
Note: The 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.
It's useful when creating class files / code files, as it's very easy to add an extra space or newline at the end of a file, which can mess up output buffering header() output. Since PHP treats an EOF like a closing ?> in a file there's no danger in relying on the EOF.
Yes, it's useful, because you can't forget odd chars like \n after closing tag (which can prevent from sending cookies etc). In Zend Framework they do not use closing tags also (as a project code standart).