0

I'm using PHP 7.1.12

I know that the closing tag of a block of PHP code automatically implies a semicolon so we do not need to have a semicolon terminating the last line of a PHP block.

The closing tag for the block will include the immediately trailing newline if one is present.

So, my question is suppose I'm having a PHP code block with few statements withing, I add ten line feeds after the last valid code line then I didn't add the closing PHP tag i.e. ?>

So, in above case will PHP remove those extra 10 line feeds or if any unwanted whitespace I've added?

2 Answers2

2

If your files are pure PHP, i.e. you are not using multiple PHP blocks in an HTML file, you should always avoid closing tags. Closing tags signify that your script is finished, and treat the next part of the code as HTML. if your HTML and PHP are separate in your codebase, you should start the file with <?php tag, and just write PHP script afterward. All the lines in that file will be treated as PHP, and only the part echoed back will be printed on the screen.

So coming back to your question, those 10 lines you've added after your last PHP-code line will be treated as part of PHP script, and will just be treated as empty lines in a script, instead of empty lines in HTML.

Some references for more information: https://softwareengineering.stackexchange.com/questions/89553/closing-tag-on-php-files https://www.sitepoint.com/should-you-close-your-php-code-tags/ https://wordpress.stackexchange.com/questions/210765/to-close-or-not-to-close-php

xRahul
  • 364
  • 5
  • 18
1

I know that the closing tag of a block of PHP code automatically implies a semicolon so we do not need to have a semicolon terminating the last line of a PHP block.

Means <?php echo '' ?> is fine.

The closing tag for the block will include the immediately trailing newline if one is present.

Means

<?php echo '' ?>
\n
\n
\n

Will output 3 line breaks.

But I think your asking, whether the following will output line breaks.

<?php echo '';
\n
\n
\n

Which it wont, for the same reason PHP comments don't get outputted.

PSR-2 guidelines 2.2. Files.

  • All PHP files MUST use the Unix LF (linefeed) line ending.

  • All PHP files MUST end with a single blank line. (not 10 ;p)

  • The closing ?> tag MUST be omitted from files containing only PHP.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106