php tags open and close
1. Introduction to PHP Tags
What are PHP tags?
PHP tags are special “start” and “end” markers that tell the server where the PHP code begins and ends. They look like this:
Anything written between <?php and ?> is treated as PHP code and will be executed by the server — not shown directly to the user.
Why does PHP need tags to run code?
Because PHP is a server-side language that is often mixed with HTML.
The browser cannot understand PHP directly — only HTML, CSS, and JavaScript.
So PHP needs tags to clearly separate itself from the normal HTML:
-
The server reads the file and looks for
<?php ... ?> -
Only the code inside these tags is processed (executed) by the PHP interpreter
-
Everything outside is treated as plain HTML and sent to the browser unchanged
In simple words:
PHP tags act like “doors” that open and close a PHP mode inside an HTML file.
2. Different Types of PHP Opening Tags
PHP supports several ways to start (open) a PHP code block. Some are recommended, while others are outdated or dependent on server settings.
✅ 1. Standard PHP Tag — Recommended and Always Safe
-
This is the official and most widely supported format.
-
Works on all servers, all PHP versions.
-
Best practice — always use this in modern PHP.
✅ 2. Short Echo Tag (<?= ... ?>) — Shortcut for echo
-
This is just a shortcut for echo, same as writing:
-
Very popular in templates and frameworks like Laravel, WordPress, etc.
-
Safe to use in PHP 5.4+ (enabled by default)
⚠️ 3. Short Open Tag (<? ... ?>) — Not recommended
-
This is shorter, but depends on server settings (
short_open_tagin php.ini). -
Might not work on some servers — not reliable.
-
Avoid using this for professional or portable code.
❌ 4. ASP-style Tags — Deprecated / Removed
-
Inspired by old ASP.NET syntax.
-
Removed in modern PHP versions — no longer supported.
-
Do not use.
✅ Summary (Best Practice)
| Tag Type | Status | Should You Use It? |
|---|---|---|
<?php ... ?> |
✅ Fully supported | ✅ Yes — Recommended |
<?= ... ?> |
✅ Safe (PHP 5.4+) | ✅ Yes — For quick output |
<? ... ?> |
⚠️ Depends on server | ❌ Better avoid |
<% ... %> |
❌ Deprecated | ❌ Never use |
3. How PHP Tags Work Inside HTML
PHP is most commonly used inside HTML files — this is what makes PHP ideal for creating dynamic web pages.
✅ Embedding PHP inside HTML pages
Example:
<?php
$mypagetitle=”Hello World! php Schools.com”;
$intro_message=”wellcome to my website !you can edite this message in the \$intro_message variable”;
$my_message=”this text is generated by php variable \$my_message .you can edit it”;
echo “<!DOCTYPE html> <html><head> <title>{$mypagetitle}</title></head>
<h1 class=\”myclassname\”>{$intro_message}</h1><p class=\”myclass-2\”>{$my_message}</p>
</body></html>”
?>
output:

-
The browser sees both HTML and the result of the PHP code.
-
Anything outside
<?php ... ?>is treated as normal HTML. -
Anything inside
<?php ... ?>is processed by the server first — then the result is sent to the browser.
✅ Switching Between HTML and PHP Blocks
You can jump in and out of PHP mode whenever you want:
Or even alternate multiple times:
-
PHP lets you mix logic + HTML very easily.
-
This style is common in WordPress, Laravel, and template engines.
Key Idea
PHP tags act like an on/off switch:
| Area | Interpreted as |
|---|---|
Outside <?php ?> |
HTML (sent directly to browser) |
Inside <?php ?> |
PHP code (processed first, then output sent) |
4. Echo vs Short Echo Tag (<?= ?>)
PHP gives you two main ways to output data to the browser:
✅ Method 1: Standard echo statement (full syntax)
-
This is the traditional and original way.
-
Always available — works in every PHP version and every server.
-
Supports multiple values:
✅ Method 2: Short Echo Tag (<?= ?>) — Shortcut for faster output
-
This is 100% equal to:
-
Much shorter and cleaner — commonly used in HTML templates.
-
Safe to use in PHP 5.4+ (it’s always enabled by default).
✅ When to Use Short Echo <?= ?>
| Situation | Recommended? |
|---|---|
| Quickly printing a value or variable | ✅ Yes |
| Inside HTML (like templates or view files) | ✅ Yes |
| In WordPress, Laravel, Blade, etc. | ✅ Yes |
| In logic-heavy PHP (like classes or loops) | ⚠️ Better use full echo |
🔧 Examples with Variables and Strings
output:

-
Clean, fast, and readable
-
Perfect for printing values directly inside HTML layout
Final Rule
| Syntax | Best for | Notes |
|---|---|---|
echo |
Any situation | Always safe, ideal for PHP logic |
<?= ?> |
Fast HTML output | Best for display, not complex code |
8. PHP Version Notes and Compatibility
Understanding how PHP versions affect tag behavior is important — especially if your code will run on different servers or hosting environments.
✅ Short Tags (<? ... ?>) and php.ini Settings
Short tags like this:
might not work on all servers because they depend on a PHP setting called:
-
In older PHP versions (before PHP 5.4), short tags were disabled by default.
-
In modern PHP versions (5.4+), short echo
<?= ?>is always enabled, but short open tags<? ?>still depend on settings. -
Many hosting providers disable short tags for security and compatibility.
✅ Safe rule → Always use:
✅ And short echo is safe:
✅ Features Available in PHP 7 and PHP 8+
Modern PHP versions improved speed, security, and coding standards — especially for tag usage and mixing PHP with HTML.
| PHP Version | Important Notes |
|---|---|
| PHP 5.3 and older | Short tags often disabled. Outdated — not recommended. |
| PHP 5.4+ | <?= ?> short echo is always enabled — safe to use. |
| PHP 7 | Major performance boost, supports scalar type hints and return types. |
| PHP 8+ | Adds modern features like match, attributes, named arguments, constructor property promotion, etc. |
✅ Modern Best Practice (PHP 7 & 8+)
-
Use
<?phpand<?= ?>only -
Avoid
<? ... ?>and never use ASP-style<% %>tags -
Omit closing
?>in pure PHP files -
PHP 7 and 8 are recommended for clean, modern, and secure code
9. Practical Examples and Mini Projects
Let’s now see how PHP tags are used in real, practical situations — this helps you understand why tags matter.
✅ Example 1: Mixing PHP with a Basic HTML Form
This is a classic example of PHP + HTML working together:
What’s happening here?
-
The form is HTML
-
When the user submits it, PHP runs inside
<?php ... ?>and displays a message -
This is the most common real-world use of PHP tags
✅ Example 2: Simple Dynamic Page Using PHP Tags
PHP is generating a dynamic value (the current date and time)
-
Everything else is static HTML
-
Only the part inside
<?php ... ?>is dynamic
What You Learned Practically
| Example | Purpose |
|---|---|
| HTML Form + PHP | PHP responds to user input |
| Dynamic Time | PHP adds live, changing content |
10. Summary and Next Steps
After going through all the sections on PHP tags, here’s a recap of what you’ve learned and what you can focus on next.
✅ What You Learned
-
PHP Tags Basics
-
PHP code runs only inside
<?php ... ?> -
PHP tags separate PHP from HTML so the server knows what to execute
-
-
Different Types of PHP Tags
-
Standard
<?php ... ?>→ always safe -
Short echo
<?= ... ?>→ quick output -
Avoid
<? ... ?>and ASP-style tags<% ... %>
-
-
Mixing PHP with HTML
-
You can switch between PHP and HTML seamlessly
-
Dynamic content is generated inside PHP blocks
-
-
Echo vs Short Echo
-
echoworks anywhere -
<?= ?>is a shortcut for output in templates
-
-
Closing Tag Rules
-
Omit
?>in pure PHP files to prevent whitespace and errors
-
-
Best Practices
-
Use standard tags, clean formatting, follow PSR-12, and focus on readability
-
-
Common Mistakes
-
“Headers already sent” errors
-
Unintended whitespace or output
-
-
Version Notes & Compatibility
-
PHP 5.4+ supports
<?= ?> -
Avoid server-dependent short tags
-
Modern PHP (7/8) offers better features and stability
-
-
Practical Usage
-
Forms, dynamic content, templates, and mini projects demonstrate real-world applications
-
✅ What to Learn After PHP Tags
Once you are comfortable with PHP tags, the next steps to become proficient in PHP are:
-
Variables, Data Types, and Operators
-
Learn how to store, manipulate, and compare data
-
-
Conditional Statements & Loops
-
if,else,switch,for,while,foreach
-
-
Functions and Reusable Code
-
Writing functions, passing parameters, returning values
-
-
Arrays and Array Functions
-
Indexed, associative, and multidimensional arrays
-
-
Working with Forms & Superglobals
-
$_POST,$_GET,$_SESSION,$_COOKIE
-
-
File Handling and Include/Require
-
Including other files, reading/writing files
-
-
Object-Oriented PHP
-
Classes, objects, inheritance, interfaces, traits
-
-
Database Basics
-
Using MySQL/MariaDB with PDO or MySQLi
-
-
PHP Frameworks (Optional)
-
Laravel, Symfony, CodeIgniter for professional web development
-
