Jump to content

Should terminating semicolons always be used?


NotionCommotion

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
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.