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

Asked

Viewed 599 times

3

When using php along with html, I know it works to omit ";" in one-line php tags, but there is some problem that this may cause?

Omitting ";" is good practice or not?

<?php algumaFuncao() ?>

or

<?php algumaFuncao(); ?>

EDIT

The question is for tags with an expression to be validated only, sorry not to specify this before.

4 answers

3


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

Example:

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

Some programmer friends criticized the lack of ; after the endif, but I’m sure no one would use a ; after the closure of a if common.

Thus:

<?php if  ($x) { ?>

<?php }; ?>

I believe you’re referring, in your question, to expressions, as a echo simple, or a function call.

It is highly recommended to use the ; in such cases.

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

Example of a Blade code:

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

Blade compiles the code that way:

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

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

0

Not mandatory (in this case), but recommended.

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

http://php.net/manual/en/language.basic-syntax.instruction-separation.php

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

On that website http://www.phptherightway.com/, you find some recommendations and good practices.

-2

After any instruction, it is necessary to use a ; for the interpreter to recognize that certain instruction ends there. After ; the interpreter processes the instruction by displaying it in the computer’s memory and, if there are no errors, passes 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 break of lines was removed because the semicolon ; is that delimits the command. And the semicolon was removed ; from 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: The use of ; in the middle text to be displayed does not affect the instruction.

Data extracted from: http://pt.wikibooks.org/wiki/Curso_de_PHP/Sintaxe_b%C3%A1sica

-2

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

<?php echo "Assim funciona" ?>

In this case, the compiler will check what is right after the text, and he sees that he has reached the end of the PHP block, so he suppresses the error that would cause and allows to be written, but as well explained by @Silvio-Andorinha php requires a ;

So it would be good to adhere to the good practices of always putting ; at the end of your php sentences.

  • 1

    Hello, thank you, would you have any sources to confirm your answer? for me is the one who best explained, with a reliable source I can mark this response as the best.

Browser other questions tagged

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