Convert PHP closing tag into comment

Active3 hr before
Viewed126 times

6 Answers


After the above, you should be able to use /* to comment out the line. If you leave the ?> intact, // cannot possibly comment out an entire line. The text following ?> could be html, which is outside the control of the PHP interpreter, so that wouldn't work.,One of the lines in my script contains a PHP closing tag inside a string. Under normal operation this does not cause a problem, but I need to comment out the line.,I have tried to comment out this line with //, /* */ and # but none of them work, the parser considers closing tag to be an actual closing tag.,The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that.

In your code:

$string = preg_replace('#<br\s* /?'.'>(?:\s*<br\s* /?'.'>)+#i', '<br />', $string);

For /* */ comments to work, you'd have to split the */ sequence too:

$string = preg_replace('#<br\s*'.' /?'.'>(?:\s*<br\s*'.' /?'.'>)+#i', '<br />', $string);
load more v

Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content. This allows PHP to be embedded in HTML documents, for example to create templates. , For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print. , If PHP is embeded within XML or XHTML the normal PHP <?php ?> must be used to remain compliant with the standards. , Using structures with conditions

<p>This is going to be ignored by PHP and displayed by the browser.</p><?php echo 'While this is going to be parsed.'; ?><p>This will also be ignored by PHP and displayed by the browser.</p>
load more v

it is 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),Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.,Optimized means that the code run faster. This is easy to prove them wrong. Profile the code and figure out that they are telling you bullshit.,I think it is recommended to newbies to avoid adding it so that they don't cause extra newline chars to be sent accidentally. Since it is not essential to have it like you have mentioned, I think the general reasoning is that better leave it off to avoid mistakes.

It's not so much a matter of performance - parsing the trailing ?> is trivial and won't make any noticable difference at all, unless you're including a million files per second.

? >
load more v

One of the big advantages here is that this PHP function accounts for many standard version numbering schemes, including alpha and beta suffixes.,When aligning multiple lines (e.g. in an associative array), use spaces instead of tabs. This is helpful when displaying the code on the web, in a GitHub commit for instance.,No debugging code can be left in place for submitted add-ons unless it is commented out, i.e. no var_dump(), print_r(), die(), and exit() calls that were used while creating the add-on, unless they are commented out.,On some servers where display_errors is disabled, and you do not have the ability to change this in the php.ini, you can often enable it with:


< ? php
echo "Here's my code!"; ?
load more v

Any closing brace MUST NOT be followed by any comment or statement on the same line.,The term "class" refers to all classes, interfaces, and traits.,Opening braces MUST be on their own line and MUST NOT be preceded or followed by a blank line.,Closing braces MUST be on their own line and MUST NOT be preceded by a blank line.

This example encompasses some of the rules below as a quick overview:

< ? php

declare(strict_types = 1);

namespace Vendor\ Package;

use Vendor\ Package\ {
   ClassA as A, ClassB, ClassC as C
use Vendor\ Package\ SomeNamespace\ ClassD as D;

function Vendor\ Package\ {

const Vendor\ Package\ {

class Foo extends Bar implements FooInterface {
   function sampleFunction(int $a, int $b = null): array {
      if ($a === $b) {
      elseif($a > $b) {
         $foo - > bar($arg1);
      } else {
         BazClass::bar($arg2, $arg3);

   final public static
   function bar() {
      // method body
load more v

Code Example

You will be able to see this text.

<!-- You will not be able to see this text. -->

You can even comment out things in
<!-- the middle of --> a sentence.

Or you can
comment out
a large number of lines.

<div class="example-class">
   Another thing you can do is put comments after closing tags, to help you find where a particular element ends. <br>
   (This can be helpful if you have a lot of nested elements.)
</div> <!-- /.example-class -->

Other "closing-convert" queries related to "Convert PHP closing tag into comment"