Current Location: Home> Latest Articles> Complete Guide to PHP Tags: Short Tags vs Long Tags

Complete Guide to PHP Tags: Short Tags vs Long Tags

M66 2025-09-18

Introduction to PHP Tags

PHP tags are used to embed PHP code into HTML or text files, indicating to the parser where to start and stop processing PHP code. Understanding PHP tags allows you to efficiently write pages that combine HTML and PHP.

Types of PHP Tags

PHP tags are mainly divided into two types:

Short Tags ()

Short tags are the most commonly used type and have a concise syntax, suitable for single-line or simple PHP code. However, some servers may have short tags disabled by default, which could lead to compatibility issues.

Example:

<?php
echo "Hello, world!";
?>

Long Tags (<?php and ?>)

Long tags are safer than short tags and are less likely to be confused with HTML code. They are suitable for multi-line or complex PHP code. Long tags work in all PHP versions and are the recommended practice.

Example:

<?php
echo "Hello, world!";
/* This is multi-line PHP code */
?>

Recommended Usage

Considering security and compatibility, it is recommended to use long tags instead of short tags. Long tags are more reliable and ensure that PHP code runs correctly in all environments.