Do I need to use a semicolon at the end of a "unique expression" in PHP?

11

Example scenario:

Let's say I have a page that is generated by includes, and 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 ways to open and close the php I see around between users of the language. The former ends with ; and the others do not:

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

and

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

Questions:

  • Should I close the code even though it is closing or would it be indifferent ?
  • Is the opening and closing of the stretch already considered processing end ?
  • Could interfere with any part of the script?
  • Is it a good practice ?
asked by anonymous 28.08.2018 / 14:40

2 answers

6

Documentation :

  

Separation of instructions

     

The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon ending the last line of a PHP block. The block closing tag will include a new line shortly after, if present.

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

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

28.08.2018 / 14:47
10
  

Should I close the code even though I'm closing the section or would it be indifferent?

Nothing, just close, if you want. The tag of PHP implies having an automatic ; fault always at the end, and only at the end. But if it is to follow a blind recommendation I would say put.

  

Is opening and closing the stretch already considered processing end?

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

  

Could this interfere with some part of the script?

In the form shown, no. If you have more rows, the latter do not need to, but the rows need.

  

Is it a good practice?

No . I do not like this good practice business, and I've already said it does. But I like code readable, obvious, easy to maintain (one day you can have another line there and you have to be careful). If normal is to have ; you should always put it, so it maintains a consistency . We do not need good practice, we need to know the fundamentals and maintain consistency, this makes code better.

Yes . If you want, you can call it a good practice to put because you avoid getting confused if you need to put more rows later. Prevents a person who does not know this from getting confused. It prevents you from looking at it all the time and diverting your thinking. So it's useful to always put ; , even if it's not necessary.

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

    
28.08.2018 / 14:49