Separação de instruções

Como no C ou Perl, o PHP requer que as instruções sejam terminadas com um ponto-e-vírgula ao final de cada comando. A tag de fechamento de um bloco de código PHP automaticamente implica em um ponto-e-vírgula; você não precisa ter um ponto-e-vírgula terminando a última linha de um bloco PHP. A tag de fechamento do bloco irá incluir uma nova linha logo após, se estiver presente.

Exemplo #1 Exemplo mostrando a tag de fechamento incluíndo uma nova linha final

<?php echo "Algum texto"?>
Sem nova linha
<?= "Mas uma nova linha agora" ?>

O exemplo acima irá imprimir:

Some textNo newline
But newline now

Exemplos de entrar e sair do modo PHP:

<?php
    
echo 'Isto é um teste';
?>

<?php echo 'Isto é um teste' ?>

<?php echo 'Nós omitimos a última tag de fechamento';

Nota:

A tag de fechamento de um bloco PHP ao final de um arquivo é opcional, e em alguns casos omiti-la é útil ao utilizar include ou require, assim espaços em branco indesejados não irão aparecer ao final dos arquivos, e ainda, será capaz de adicionar cabeçalhos à resposta. Também é útil se usar o buffer de saída, e você não gostaria de um espaço em branco indesejado ao final das partes geradas por arquivos incluídos.

add a note add a note

User Contributed Notes 2 notes

up
116
Krishna Srikanth
17 years ago
Do not mis interpret

<?php echo 'Ending tag excluded';

with

<?php echo 'Ending tag excluded';
<
p>But html is still visible</p>

The second one would give error. Exclude ?> if you no more html to write after the code.
up
3
anisgazig at gmail dot com
3 years ago
At the end of each statement,we will use a semicolon to seperate each statement.Php closing tag autometically implies a semiclon.So last line of a statment do not require a statement.

<?php echo "first statement";
      echo
"second statement"
?>

Closing tag is optional and some cases omitting the closing tag is very helpful.
1.using include or require function
2.unwanted whitespace will not occur at the end of file
3.add header any time

<?php echo "omitting closing tag";
To Top