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.

PHP include un tag scurt echo <?=, care este o versiune prescurtată pentru <?php echo.

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.

<?php
echo "Hello world";

// ... more code

echo "Ultima declarație";

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

add a note add a note

User Contributed Notes 2 notes

up
-11
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
-75
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