PHPerKaigi 2019

Tag-uri PHP

Când PHP parsează un fișier, se uită după tag-urile de deschidere și închidere, acestea sunt <?php și ?>, care indică când PHP începe și termină interpretarea codului dintre ele. Parsarea în această manieră permite PHP să fie încorporat în tot felul de documente, pentru că orice este în afara perechii de tag-uri de deschidere și închidere este ignorat de către parsatorul PHP.

De asemenea, PHP permite tag scurt de deschidere <? (nu se încurajează folosirea lui deoarece este valabil numai activând directiva short_open_tag din fișierul de configurare php.ini, sau dacă PHP a fost configurat cu opțiunea --enable-short-tags ).

Dacă un fișier conține numai cod PHP, este de preferat omiterea tag-ului de închidere PHP de la sfârșitul paginii. Aceasta previne spațiu gol accidental sau noi rânduri adăugate după tag-ul de închidere PHP, ceea ce ar cauza efecte nedorite deaorece PHP va începe buferizarea ieșirii când nu este nici o intenție din partea programatorului să trimită vre-o ieșire la acel punct din script.

echo "Hello world";

// ... more code

echo "Ultima declarație";

// script-ul se termină aici fără tag de închidere

Istoricul schimbărilor
Versiune Descriere
7.0.0 Tag-urile ASP <%, %>, <%= și tag-ul de stil script <script language="php"> sunt excluse din PHP.
5.4.0 Tag-ul <?= este mereu disponibil indiferent de setarea ini short_open_tag.

add a note add a note

User Contributed Notes 6 notes

crazytonyi at gmail dot com
3 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 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 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.
Mark Clements (
11 months ago
Closing PHP tags are recognised within single-line comments:

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

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

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

/* Code will not end here ?> as closing tags are ignored inside C-style comments. */
pl at dot pl
4 days ago
Omit closing tag ?> always whenever you can

<!DOCTYPE html><head>
<title><?php include'varia.php'; echo$title?></title>

= 'Welcome';
[new line]        //other words: an extra 'Enter' is guilty

and you get:

Welcome - at the browser label

source won't tell you what happened - there will be fine:

<!DOCTYPE html><head>

Now imagine, what else can go wrong because of it? Everything, as Murphy said.
And you will look for the answer why...? And where...?
It's just simplest example.
raoni at gmail dot com
10 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)
dave at juuce dot com
1 year ago
// ///////
// comments

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

// //////
// comments

Add a space character after <?php on the first line corrects the problem.
purkrt at gmail dot com
4 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