PHPKonf Istanbul PHP Conference 2019 - Call for Papers

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 dessa maneira, permite o PHP ser incluído em vários tipos de documentos, pois tudo que está fora dessas tags é ignorado pelo interpretador do PHP.

O PHP também permite a tag curta <? (cujo uso é desencorajado pois essa opção está disponível somente quando habilitada na diretiva short_open_tag no arquivo de configuração php.ini, ou quando o PHP tiver sido compilado com a opção --enable-short-tags ).

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

Changelog
Versão Descrição
7.0.0 As tags ASP <%, %>, <%= e a script tag <script language="php"> foram removidos do PHP.
5.4.0 A tag <?= sempre está disponível, independente do da configuração short_open_tag ini.

add a note add a note

User Contributed Notes 5 notes

up
41
crazytonyi at gmail dot com
2 years ago
Regarding earlier note by @purkrt :

> I would like to stress out that the opening tag is "<?php[whitespace]", not just "<?php"

This is absolutely correct, but the wording may confuse some developers less familiar with the extent of the term "[whitespace]".

Whitespace, in this context, would be any character that generated vertical or horizontal space, including tabs ( \t ), newlines ( \n ), and carriage returns ( \r ), as well as a space character ( \s ). So reusing purkrt's example:

<?php/*blah*/ echo "a"?>

would not work, as mentioned, but :

<?php /*php followed by space*/ echo "a"?>

will work, as well as :

<?php
/*php followed by end-of-line*/ echo "a"?>

and :

<?php    /*php followed by tab*/ echo "a"?>

I just wanted to clarify this to prevent anyone from misreading purkrt's note to mean that a the opening tag --even when being on its own line--required a space ( \s ) character. The following would work but is not at all necessary or how the earlier comment should be interpreted :

<?php
/*php followed by a space and end-of-line*/ echo "a"?>

The end-of-line character is whitespace, so it is all that you would need.
up
6
Mark Clements (kennel17.co.uk)
7 months ago
Closing PHP tags are recognised within single-line comments:

    <?php
   
// Code will end here ?> This is output as literal text.

    <?php
   
# Same with this method of commenting ?> This is output as literal text.

However they do not have an effect in C-style comments:

    <?php
   
/* Code will not end here ?> as closing tags are ignored inside C-style comments. */
   
?>
up
-17
raoni at gmail dot com
7 months ago
If short_open_tag = Off

<?php/*php not followed by space*/ echo "a"?>
This will not be parsed by php

If short_open_tag = On

<?php/*php not followed by space*/ echo "a"?>
This will be parsed by php, but it throw "Parse error: Syntax error ..." because because 'php' will be assumed as a constant

<?/*? not followed by space*/ echo "a"?>
This will be parsed by php,  and work fine.

(Tested in php 7.1)
up
-35
dave at juuce dot com
11 months ago
<?php
// ///////
// comments

Found that single line comments cause issues and received "notice: use of undefined constant php - assumed 'php'" for line 1 of file.

<?
php<space>
// //////
// comments

Add a space character after <?php on the first line corrects the problem.
up
-123
purkrt at gmail dot com
3 years ago
I would like to stress out that the opening tag is "<?php[whitespace]", not just "<?php". While this might seem blatantly obvious, I thought for some time that

<?php/*blah*/ echo "a"?>

would work, and it does not; the comment does not work as whitespace. I've run into this while converting some older code with short open tag.
To Top