NotionCommotion Posted October 16, 2016 Share Posted October 16, 2016 I had the following script in a file. I then decided to updated all my files and remove the closing ?>. Later found I had an error. <?php // bla bla bla $site=new site(); $site->execute() ?> But why did it originally work? My lack of a closing semicolon was simple a oversight and not my intention. Then I read http://php.net/manual/en/language.basic-syntax.instruction-separation.php: The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. Okay, now I know. But why does PHP have such a "feature" of not needing a semicolon at the end of a PHP block? Does it provide any value? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 16, 2016 Share Posted October 16, 2016 It's sometimes useful for one-liners as shown in the examples in the manual. Quote Link to comment Share on other sites More sharing options...
kicken Posted October 17, 2016 Share Posted October 17, 2016 Since they are required pretty much everywhere one might as well get in the habit of always using one. It can be nice to omit them when using PHP as a templating language, for example <?php foreach ($StaffList as $sta): ?> <tr> <td><?=o($sta['staffFullname'])?></td> <td><?=o($sta['staffUsername'])?></td> <td><?=o($sta['jobTitle'])?></td> <td><?=o($sta['permissionGroupName'])?></td> </tr> <?php endforeach; ?> I generally add them anyway out of habit but occasionally I'll forget and it's nice to not have PHP complain about it. Side-rant: People intentionally omitting semi-colons in Javascript due to the ASI feature annoys the crap out of me. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 17, 2016 Share Posted October 17, 2016 Side-rant: People intentionally omitting semi-colons in Javascript due to the ASI feature annoys the crap out of me.Except that can actually be a real problem. At least with PHP the rule is clear and narrow: only if it's the last statement. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 17, 2016 Author Share Posted October 17, 2016 Okay, "maybe" they would be nice when using PHP as a template engine. I personally, however, would rather have PHP complain about it as a syntax error. Thanks Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 17, 2016 Share Posted October 17, 2016 As expressed by others above - the habit of always ending a line with a semi is a better one that intentionally looking for the opportunity to omit a semi because of a closing PHP tag. What happens when one does maintenance to add a couple more lines and then wonders why his script is giving him errors because he forgot to edit that hanging chad? PS - I rarely ever ever use a closing php tag so I did not even know this was an issue. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 17, 2016 Author Share Posted October 17, 2016 PS - I rarely ever ever use a closing php tag so I did not even know this was an issue. That was exactly what caused my problem. I had a file which worked perfectly, but then removed the trailing closing php tag, and thinking nothing could ever go wrong (yea, right), didn't test it, and discovered the error the hard way. Quote Link to comment 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.