CanMikeF1 Posted February 4, 2022 Share Posted February 4, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/314503-closing-required-for-php8/ Share on other sites More sharing options...
ginerjm Posted February 4, 2022 Share Posted February 4, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314503-closing-required-for-php8/#findComment-1593837 Share on other sites More sharing options...
CanMikeF1 Posted February 4, 2022 Author Share Posted February 4, 2022 Thanks. Good to know I'm not going nuts. Shall read up on 'heredocs operand' for more information. Quote Link to comment https://forums.phpfreaks.com/topic/314503-closing-required-for-php8/#findComment-1593840 Share on other sites More sharing options...
maxxd Posted February 4, 2022 Share Posted February 4, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314503-closing-required-for-php8/#findComment-1593842 Share on other sites More sharing options...
gizmola Posted March 2, 2022 Share Posted March 2, 2022 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314503-closing-required-for-php8/#findComment-1594182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.