Is it necessary to use semicolons at the end of a "single expression" in PHP?

Asked

Viewed 138 times

12

Example scenario:

Let’s assume I have a page that’s generated by includes, and it even has a loop:

<? require_once 'classes/classe1.class.php'; ?>

<html>

<head>
    <? include_once 'html/head.php'; ?>
</head>

<body>

    <? foreach ($array as $v) { ?>

        <tr>
            <td><? echo $v['X'] ?></td>
            <td><? echo $v['Y'] ?></td>
        </tr>

    <? } ?>

</body>
</html>

In the example, I show 2 forms to open and close the php that I see around users of the language. The first end with ; and the others not:

<? require_once 'classes/classe1.class.php'; ?>
<? include_once 'html/head.php'; ?>

and

<td><? echo $v['X'] ?></td>
<td><? echo $v['Y'] ?></td>

Doubts:

  • Should I or should I not close the code even if you are closing the snippet or would be indifferent?
  • The opening and closing of the stretch already is considered end of processing?
  • That could interfere somewhere in the script?
  • Is a good practice?
  • In the last row of a block, it is not necessary to put the ;, but if you have a line below you need to.

  • If you do not close it will look for the instruction ahead in case it is the closing of the php script that it interprets as an end. It does not interfere in anything

  • Read the official documentation : http://php.net/manualen/language.basic-syntax.instruction-separation.php

  • I believe it’s not good practice, in case you’re going to tamper with the code in the future, you can forget about putting the ; and cause waste of time looking for the problem.

2 answers

11

Should I or should I not close the code even if I am closing the section or it would be indifferent?

You can only close if you want. A tag of PHP implies having a ; automatic always at the end, and only at the end. But if it is to follow a blind recommendation I would say to put.

The opening and closing of the section is already considered processing end?

Yes, it closes the code there. Since PHP is a language of script It makes sense to have several processing lines started and closed within the HTML template. From the syntactic point of view each one is independent. The ; is implicit and placed by the PHP compiler.

This could interfere in some part of the script?

In the presented form no. If you have more lines, the last one does not need, but the previous ones need.

Is a good practice?

Not. I don’t like this business of good practice, and I said it doesn’t matter. But I like readable code, obvious, easy to maintain (one day there may be another line there and you will have to be careful). If the normal is to have the ; you should put it always, so keep a consistency. We don’t need good practice, we need to know the fundamentals and maintain consistency, this makes better code.

Yes. If you want, you can call it good practice to put it, because it avoids confusing yourself if you need to put more lines later. It prevents a person who does not know it from getting confused. It avoids you looking at it to find weird every time and deflect thought. So it is useful to always put the ;, even though it’s not necessary.

Good practice depends on context and point of view, so they are complicated. People think they are cake recipe.

9


Documentation:

Separation of instructions

The closing tag of a block of PHP code automatically implies a semicolon; you don’t need to have a semicolon ending the last line of a PHP block. The block closing tag will include a new line right after, if it is present.

That is, the expression that immediately precedes the closing of the PHP block does not necessarily need to have the semicolon.

Just be careful that <? ?> is the short tag and it is no longer recommended to use it. Always prefer <?php ?>.

Should I or should I not close the code even if I am closing the section or it would be indifferent?

To the last expression before closing the PHP block, the use of the semicolon is indifferent. For all others, there is masterc**d yes, it is mandatory.

The opening and closing of the section is already considered processing end?

Yes. The end of the block, ?>, is already analyzed as the end of the expression, so the use of semicolon becomes optional.

This could interfere with some part of the script?

No, not at all.

Is a good practice?

Whether it’s indifferent, it can’t be, or not, good practice.

  • Come on, man, you’ve read about it short tag, then the <?= also not or is standard?

  • @Rbz no, the <?= is not a short tag. I’ve added links to questions that address this.

  • This short tag is indicating that the output of the script is to send to stdout or print, is an abbreviation for echo. <?= (5 + 5) ? > equals <?php echo 5+5; ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.