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.– Roberto de Campos
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
– Rafael Salomão
Read the official documentation : http://php.net/manualen/language.basic-syntax.instruction-separation.php
– Rafael Salomão
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.– Roberto de Campos
Possible duplicate of Is there a problem omitting the semicolon in a php tag with only one expression
– Woss