Теги PHP

Коли PHP обробляє файл, вона шукає відкриваючі та закриваючі теги, тобто <?php та ?>, які говорять PHP, що між ними потрібно починати і завершувати інтерпретацію кода. Обробка в такій манері дозволяє PHP бути вбудованою у різного роду документи, оскільки код, за межами пари з відкриваючим та закриваючим тегами, ігнорується обробником PHP.

PHP також дозволяє короткий відкриваючий тег <? (в цьому випадку закриваючий тег не змінюється, але використовуйте його обережно, оскільки він доступний, тільки якщо його підключено через директиву short_open_tag у конфігураційному файлі php.ini, або якщо PHP було сконфігуровано з параметром --enable-short-tags ).

Якщо файл має лише PHP-код, краще не вказувати закриваючий тег PHP в кінці файла. Це запобігає випадковості додавання пробіла або нового рядка після закриваючого тега PHP, що може спричиняти небажаний ефект, оскільки PHP починає буферизацію виводу, коли програміст не має наміру будь-що виводити в своєму скрипті.

<?php
echo "Hello world";

// ... ще код

echo "Останній вивід";

// тут скрипт завершується без додавання закриваючого тега PHP

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