Tags PHP

Quando o PHP interpreta um arquivo ele procura pelas tags de abertura e fechamento, <?php e ?>, que dizem ao PHP para iniciar ou parar a interpretação do código entre elas. A interpretação assim permite ao PHP ser incluído em vários tipos de documentos, pois tudo que está fora destas tags é ignorado pelo interpretador do PHP.

PHP inclui uma tag curta echo <?= que é uma forma abreviada mais verbosa para<?php echo.

Exemplo #1 Abrindo e fechando tags PHP

1.  <?php echo 'se você deseja rodar código PHP dentro de documentos XHTML ou XML,
                utilize essas tags'
?>

2.  Você pode utilizar a tag curta <?= 'imprima essa string' ?>.
    É o equivalente de <?php echo 'print this string' ?>.

3.  <? echo 'Este código está entre tags curtas, mas somente funcionará '.
            'se short_open_tag estiver ativo'; ?>

Tags curtas (exemplo 3) estão disponíveis por padrão mas podem ser desabilitadas através da diretiva short_open_tag no arquivo de configuração php.ini, ou desabilitadas por padrão se o PHP for compilado usando a configuração --disable-short-tags.

Nota:

Como as tags curtas podem ser desabilitadas, é recomendável usar apenas as tags normais (<?php ?> and <?= ?>) para maximizar a compatibilidade.

Se um arquivo for código PHP puro, é preferível omitir a tag de fechamento no final do arquivo. Prevenindo a existência de espaços ou linhas em branco após a tag, que podem causar efeitos indesejáveis, por que o PHP iniciará o buffer de saída quando não existir intenção do programador de enviar alguma saída neste ponto do script.

<?php
echo "Hello world";

// ... mais código

echo "última instrução";

// o script termina aqui, sem tag de fechamento PHP

add a note add a note

User Contributed Notes 2 notes

up
-4
admin at bharatt dot com dot np
2 years ago
You may want to know that removing semicolon is optional sometime but need to know the condition when it can be removed and when it can't be.
-------------------------------------------------------------------------
// Example 1: PHP script with closing tag at the end.
<?php

// php code

// you can remove semicolon
mysqli_close( $db )
?>

// Example 2: PHP script without closing tag at the end.
<?php

// php code

// you can't remove semicolon
mysqli_close( $db )

-----------------------------------------------------------------------
up
-64
anisgazig at gmail dot com
3 years ago
Everything inside a pair of opening and closing tag is interpreted by php parser. Php introduced three types of opening and closing tag.
1.Standard tags
<?php ?>
2.Short echo tag
<?=  ?>
here, <?=  is equivalent meaning of "<?php echo"
3.Short tag
<?   ?>

Standard tag and short echo tag are alwayes available.
But short tag can be disabled either via the short_open_tag php.ini configuration file directive, or are disabled by default if PHP is built with the --disable-short-tags configuration.

If a file contains only PHP code, it is preferable to omit the PHP closing tag at the end of the file.
This prevents accidental whitespace or new lines being added after the PHP closing tag
To Top