0
0
PHPprogramming~5 mins

PHP tags and embedding in HTML

Choose your learning style9 modes available
Introduction

PHP tags let you write PHP code inside an HTML file. This helps make web pages that can change and show different things.

You want to show the current date or time on a webpage.
You need to display different messages based on user actions.
You want to include data from a database inside a webpage.
You want to mix HTML layout with PHP logic in one file.
Syntax
PHP
<?php
  // PHP code here
?>

The <?php ?> tags tell the server to run the code inside as PHP.

You can put PHP code anywhere inside an HTML file between these tags.

Examples
This example shows PHP code inside HTML that prints a message.
PHP
<html>
<body>
  <?php echo "Hello, world!"; ?>
</body>
</html>
PHP code inside a paragraph tag prints the current date.
PHP
<p>Today is <?php echo date('Y-m-d'); ?>.</p>
PHP sets a variable, then HTML uses PHP to show it inside a heading.
PHP
<?php
  $name = "Alice";
?>
<h1>Welcome, <?php echo $name; ?>!</h1>
Sample Program

This webpage uses PHP tags inside HTML to show the current year and a greeting based on the time.

PHP
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>PHP Tags Example</title>
</head>
<body>
  <h1>My Webpage</h1>
  <p>Current year is <?php echo date('Y'); ?>.</p>
  <?php
    $hour = date('H');
    if ($hour < 12) {
      echo "<p>Good morning!</p>";
    } else {
      echo "<p>Good afternoon!</p>";
    }
  ?>
</body>
</html>
OutputSuccess
Important Notes

Always start PHP code with <?php and end with ?>.

PHP code runs on the server before the page shows in the browser.

You can switch between HTML and PHP as many times as you want in one file.

Summary

PHP tags let you add PHP code inside HTML files.

Use <?php ?> to start and end PHP code.

This helps make webpages that can change content dynamically.