Is there any problem in omitting the semicolon in a php tag with an expression only

3

When using php along with html, I know it works by omitting the ";" in one line php tags, but is there a problem that this can lead to?

Omit the ";" is it a good practice or not?

<?php algumaFuncao() ?>

or

<?php algumaFuncao(); ?>

EDIT

Doubt is for tags with an expression to be validated only, sorry for not specifying this before.

    
asked by anonymous 21.03.2014 / 16:49

4 answers

3

In cases like expressions using the alternate syntax, I do not use ; .

Example:

<?php foreach($array as $value) : ?>
<?php endforeach ?>

Some programmer friends criticized the lack of ; after endif , however I'm sure no one would use a ; after closing a if common.

So:

<?php if  ($x) { ?>

<?php }; ?>

I think you're referring in your question to expressions, such as a simple echo , or a function call.

We strongly recommend using ; in these cases.

I often look at the frameworks source code, so I can try to improve my coding standard. And looking at the source code of Laravel 4 , I could see what it does when using the Blade syntax.

Example of a Blade code:

Meu nome é {{ $nome }} e tenho {{ $idade }} anos

Blade compiles the code this way:

Meu nome é <?php echo $nome; ?> e tenho <?php echo $idade; ?>

Then we realized that this framework was concerned with putting ; at the end of the expression.

    
25.09.2015 / 17:12
0

It is not required (in this case), however, it is recommended.

  

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

link

It is not necessary in this case, because ?> is in charge of "finishing" the line.

At link , you'll find some recommendations and best practices.

    
21.03.2014 / 17:38
-2

After any instruction, it is necessary to use one; so that the interpreter recognizes that a certain instruction ends there. After ; the interpreter processes the statement by displaying it in the computer's memory and, if there are no errors, it goes to the next instruction.

Example:

<?php
   echo "Olá como estás?";
   echo "Tudo bem contigo?";
?>

or

<?php echo "Olá como estás?"; echo "Tudo bem contigo?" ?>

Note that in this second case the line break was dropped because the semicolon; it delimits the command. And the semicolon was removed; of the last command because soon after the php tag is closed, it is not a good practice, but it works.

<?php echo "Se não usar ponto e vírgula (;) o interpretador irá exibir um erro"; ?>

Note: Please note that the use of; in the middle text to be displayed does not affect the statement.

  

Data extracted from:    link

    
21.03.2014 / 17:05
-2

It is not a good practice to omit, although it works as

<?php echo "Assim funciona" ?>

It turns out in that case that the compiler will check what it has right after the text, and it sees that it has reached the end of the PHP block, so it suppresses the error that would cause and allows to be written, however as well explained by @ Silvio-Andorinha php requires ;

So, in this case, it would be good to stick to the good practices of always putting ; at the end of your sentences in php.

    
21.03.2014 / 17:36