PHP Tags

PHP tags are special markers that tell the PHP parser where PHP code begins and ends within a file. These tags allow you to embed PHP code within HTML documents, enabling server-side processing before the content is sent to the browser.

Basic PHP Tags

The standard PHP opening and closing tags are <?php and ?>. All PHP statements must be written between these tags to be processed by the server.

Syntax

<?php
// PHP statements go here
?>

Example

<?php
echo "Hello World";
?>
Hello World

Short Echo Tags

PHP provides a shorthand syntax <?= for echoing content, which is equivalent to <?php echo. This feature is always available regardless of the short_open_tag setting.

Example

<?= "Hello World using short echo tag"; ?>
Hello World using short echo tag

Short Tags (Deprecated)

PHP previously allowed using <? instead of <?php when the short_open_tag directive was enabled in php.ini. However, this practice is discouraged for portability reasons.

Note: Short tags are disabled by default and not recommended for production environments as they may conflict with XML declarations.

Discontinued Tag Styles

The following tag styles have been removed since PHP 7.0 −

  • ASP-style tags: <% and %>
  • Script tags: <script language="php"> and </script>

Best Practices

Tag Type Usage Recommendation
<?php ?> Standard tags Always use for compatibility
<?= ?> Short echo Use for simple output
<? ?> Short tags Avoid in production

Conclusion

Always use the standard <?php tags for maximum compatibility and portability. Use short echo tags <?= for simple output statements, but avoid deprecated short tags in production environments.

Updated on: 2026-03-15T09:18:12+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements