PHP Tags & Embedding in HTML: Learn How to Write PHP Code Inside HTML Easily
Introduction
When you start learning PHP, one of the first things you’ll encounter is how to embed PHP inside HTML pages.
PHP (Hypertext Preprocessor) is a server-side scripting language that works perfectly with HTML to create dynamic web pages — pages that change based on data, user input, or database content.
This guide will teach you how PHP and HTML work together, the types of PHP tags, and how to correctly place your PHP code inside an HTML document.
Understanding PHP and HTML Integration
HTML is used to structure a webpage (text, images, buttons, etc.), while PHP adds logic and functionality — for example:
Displaying current date/time dynamically
Showing user information from a database
Handling form inputs
Controlling access based on user login
When the browser requests a .php file, the server executes PHP code first, and sends the final HTML output to the browser. The PHP code itself is not visible to the user.
PHP Opening and Closing Tags
Every PHP script is enclosed between special tags that tell the server where PHP code begins and ends.
Standard PHP Tags
<?php echo "Hello, World!"; ?> <?php→ Opening PHP tag?>→ Closing PHP tagEverything inside these tags is treated as PHP code.
Short Open Tags (Not recommended)
<? echo "Hello, World!"; ?> Short tags (<? ... ?>) are disabled by default in most servers for security reasons. Always prefer the standard tag <?php.
ASP-style Tags (Deprecated)
<% echo "Hello, World!"; %>These are no longer supported. Avoid using them in modern PHP.
Script Tag
<script language="php"> echo "Hello from script tag!"; </script> This style is rare and outdated — supported by very few configurations.
Embedding PHP Inside HTML
PHP can be placed anywhere inside HTML files. The file should be saved with the .php extension.
Example 1: Basic Embedding
<!DOCTYPE html>
<html>
<head>
<title>PHP in HTML Example</title>
</head>
<body>
<h1>Welcome!</h1>
<p>The current year is: <?php echo date("Y"); ?></p>
</body>
</html>
Output:
Welcome! The current year is: 2025 Here, PHP is only used where dynamic content is needed.
Example 2: Conditional Logic Inside HTML
<!DOCTYPE html>
<html>
<body>
<?php
$hour = date("H");
if ($hour < 12) {
echo "<h2>Good Morning!</h2>";
} else {
echo "<h2>Good Afternoon!</h2>";
}
?>
</body>
</html>
Output (based on time):
Good Morning! OR Good Afternoon!Example 3: Mixing HTML and PHP Blocks
<?php
$name = "Arvinder";
?>
<html>
<body>
<h1>Welcome <?php echo $name; ?>!</h1>
<p>This page was generated using PHP.</p>
</body>
</html>
This example shows how variables declared in PHP can be used inside HTML.
Important Notes
- Always save PHP files with
.phpextension. - PHP code runs only on servers with PHP installed (e.g., XAMPP, WAMP, or a hosting server).
- PHP code is executed before HTML is sent to the browser.
- You can use multiple PHP blocks in one HTML page.
Best Practices
Use
<?php ?>— the full tag — for compatibility.Indent and comment your PHP code properly.
Avoid mixing too much PHP logic inside HTML; separate logic into functions or includes.
Use
echoorprintto output dynamic values inside HTML.