LeonLatex Posted August 25, 2022 Share Posted August 25, 2022 (edited) 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 August 25, 2022 by LeonLatex Quote Link to comment https://forums.phpfreaks.com/topic/315237-close-php/ Share on other sites More sharing options...
Barand Posted August 25, 2022 Share Posted August 25, 2022 Try it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599828 Share on other sites More sharing options...
ginerjm Posted August 25, 2022 Share Posted August 25, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599830 Share on other sites More sharing options...
LeonLatex Posted August 25, 2022 Author Share Posted August 25, 2022 52 minutes ago, Barand said: Try it. I have, and so far it works fine, but can I meet some problems in the future I don't see by not closing one or more blocks? Or not closing PHP at all at the end of the document? Quote Link to comment https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599832 Share on other sites More sharing options...
Phi11W Posted August 26, 2022 Share Posted August 26, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599859 Share on other sites More sharing options...
LeonLatex Posted August 26, 2022 Author Share Posted August 26, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315237-close-php/#findComment-1599864 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.