Jump to content

Recommended Posts

As I have understood it's not normal or necessary anymore and after the new standard(?) to close PHP (?>). So if I got a PHP file with a mix of HTML and PHP I can just open for PHP (<?php)?

In other words. Can I just leave it open through the whole file?

Edited by LeonLatex
Link to comment
https://forums.phpfreaks.com/topic/315237-close-php/
Share on other sites

I make it a habit to stay in PHP mode all the time.  To avoid switching in and out when I am coding in JS or HTML I use the heredocs operation which makes for very easy coding and allows me to easily embed php vars in the html code without concern.  Read up on it.  

Same thing with a db connection - no need to close it down since php will do the clean up when you exit the script.

Link to comment
https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599830
Share on other sites

16 hours ago, LeonLatex said:

can I meet some problems in the future I don't see by not closing one or more blocks?

You probably face more problems if you do explicitly close PHP blocks. 

Unless those "?>" characters are the very last in the file - and they almost certainly won't be, because we naturally hit [Enter] after typing anything on a line - then the whitespace after them will be sent as part of the HTTP response.  If, somewhere later on in your code, you try to alter one of the HTTP Headers - BOOM!  The web server will complain that it's already sent some "content" - you can't see anything, but the server insist it's there. 

Omit the closing tags completely and all that whitespace will just be sitting between PHP tokens, where it matters not a jot
Some coding standards actually enforce this. 

Regards, 
   Phill  W.

 

Link to comment
https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599859
Share on other sites

4 hours ago, Phi11W said:

You probably face more problems if you do explicitly close PHP blocks. 

Unless those "?>" characters are the very last in the file - and they almost certainly won't be, because we naturally hit [Enter] after typing anything on a line - then the whitespace after them will be sent as part of the HTTP response.  If, somewhere later on in your code, you try to alter one of the HTTP Headers - BOOM!  The web server will complain that it's already sent some "content" - you can't see anything, but the server insist it's there. 

Omit the closing tags completely and all that whitespace will just be sitting between PHP tokens, where it matters not a jot
Some coding standards actually enforce this. 

 

 

Thanks for a good explanatory answer, Phill. Such good answers are very nice to get. They are confirming and complementary at the same time for what you already know, but because insecurity is afraid of believing in yourself or afraid to ask so that you don't screw yourself up completely. That's why I'm happy for such answers. I may not be the one who is most afraid of asking stupid questions, but even I have a built-in barrier. I must become better at trusting myself. I haven't put the list too high for that. I know. But thank you.

Link to comment
https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599864
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.