Jump to content

Closing '?>' required for php8


CanMikeF1
 Share

Recommended Posts

So this something that, though not important, is a small detail that been bothering me.

Every PHP source I've ever written begins and ends with <?php (code) ?>.

I thought I read somewhere the ending ?> may not be required? Maybe since PHP7.5?

I'm using PHP8, dropped the '?>' in a couple of source codes and seems OK?

I'm I nuts or is this a thing?

 

 

Link to comment
Share on other sites

It is standard practice to not close the php mode.  That seems to be mentioned here quite often.  I think it also may cause issues with included modules possibly.  It truly is not required and hasnt' been since I began using php some 7+ years ago.

That being said - as a noob you may wonder how you can write non-php code easily.  I suggest reading up on the 'heredocs' operand in the manual.  Comes in very handy to write your big blocks of html or js code and not worrying about entering and leaving php mode.

Link to comment
Share on other sites

To clarify, it's standard practice to omit the closing tag in files that only include PHP. If you're mixing your PHP into HTML (which is it's own kettle of fish) you can either close the PHP block before you write your HTML or - as ginerjm mentioned - you can use a heredoc and then output the markup from that variable.

Link to comment
Share on other sites

  • 4 weeks later...
On 2/4/2022 at 2:45 PM, maxxd said:

To clarify, it's standard practice to omit the closing tag in files that only include PHP. If you're mixing your PHP into HTML (which is it's own kettle of fish) you can either close the PHP block before you write your HTML or - as ginerjm mentioned - you can use a heredoc and then output the markup from that variable.

 

This is true, but a better rule of thumb is that a script should never end with a php end tag.  So you should always omit it.

 

For the OP, just to explain the reasoning behind this: 

The PHP interpreter will automagically close off any PHP script or block when a file ends, removing all whitespace.  If however, you create a php script with a standard block like this:

 

<?php

// Some PHP code

?>

// some whitespace here, or even this "comment" which is actually outside the PHP block and will be treated as text by the parser!

Now you include this in another script ... and what happens?  PHP sees the end block, something after it, and starts output. This was (and probably still is) a frequent cause of the "Headers already sent" error that people encounter when using sessions or trying to use the header() function to set HTTP headers.  When non-buffered output is generated by PHP, the HTTP header gets sent first, so at that point it is too late to set a cookie or any other HTTP header.

 

The solution of leaving off the php end tag has been a long time best practice going back to version 5.x at least.  

This recommendation was formalized in PSR-2 (since deprecated in favor of PSR-12) so you might want to take a look at the other code standard recommendations from PSR-12, as it's a great set of standards and guidelines for writing and formatting your code.  There are also tools that integrate with some of the more used PHP code editors that can reformat your code to fit some or all of these standards.

  • Great Answer 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.