Skip to content

Adding PHP to HTML

Adding PHP to HTML

PHP is a server-side scripting language that can be embedded within HTML to create dynamic web pages. Since PHP is executed on the server before being sent to the client’s browser, it allows for processing logic, database interactions, and dynamic content generation.


1. Basic Syntax for Embedding PHP in HTML

The PHP code is placed inside <?php … ?> tags within an HTML document. When the page is requested, the PHP script executes on the server, and only the resulting HTML is sent to the browser.

Example: Embedding PHP in HTML

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>PHP in HTML</title>

</head>

<body>

    <h1>Welcome to My Website</h1>

    <p>Today’s date is: <?php echo date(“Y-m-d”); ?></p>

</body>

</html>

Output in Browser:

Welcome to My Website 

Today’s date is: 2025-02-03 

  • The <?php echo date(“Y-m-d”); ?> executes the PHP function date() and inserts the output inside the HTML.

2. Writing PHP Inside HTML Elements

PHP can be used inside different HTML elements, such as headings, paragraphs, and lists.

Example: Using PHP Inside HTML Elements

<!DOCTYPE html>

<html>

<head>

    <title>PHP in HTML Elements</title>

</head>

<body>

    <h1><?php echo “Dynamic Page Title”; ?></h1>

    <p><?php echo “This paragraph is generated by PHP.”; ?></p>

    <ul>

        <li><?php echo “Item 1”; ?></li>

        <li><?php echo “Item 2”; ?></li>

        <li><?php echo “Item 3”; ?></li>

    </ul>

</body>

</html>

  • Here, <?php echo “Dynamic Page Title”; ?> outputs a dynamic title inside an <h1> tag.
  • Each <li> element is dynamically generated using PHP.

3. Using PHP Variables Inside HTML

PHP variables can be used to store and display dynamic content inside an HTML page.

Example: Using PHP Variables in HTML

<?php

$siteTitle = “My PHP Website”;

$welcomeMessage = “Welcome to my dynamic website!”;

?>

<!DOCTYPE html>

<html>

<head>

    <title><?php echo $siteTitle; ?></title>

</head>

<body>

    <h1><?php echo $welcomeMessage; ?></h1>

    <p>This site was built using PHP and HTML.</p>

</body>

</html>

  • The $siteTitle and $welcomeMessage variables store text that is inserted into the HTML dynamically.

4. Using PHP with HTML Forms

PHP is commonly used with forms to process user input dynamically.

Example: Processing a Form with PHP

<!DOCTYPE html>

<html>

<head>

    <title>PHP Form Handling</title>

</head>

<body>

    <form method=”post”>

        Name: <input type=”text” name=”username”>

        <input type=”submit” value=”Submit”>

    </form>

    <?php

    if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

        $name = $_POST[‘username’];

        echo “<p>Hello, ” . htmlspecialchars($name) . “!</p>”;

    }

    ?>

</body>

</html>

  • This form allows users to enter their name, and PHP processes and displays it dynamically.

5. Using Loops and Conditionals in PHP with HTML

PHP control structures such as loops and conditionals can be used to generate dynamic HTML content.

Example: Using a Loop to Generate an HTML List

<!DOCTYPE html>

<html>

<head>

    <title>Loop Example</title>

</head>

<body>

    <h1>Items List</h1>

    <ul>

        <?php

        $items = [“Apple”, “Banana”, “Cherry”];

        foreach ($items as $item) {

            echo “<li>$item</li>”;

        }

        ?>

    </ul>

</body>

</html>

  • The foreach loop generates a <li> item for each element in the $items array.

Example: Using an If Statement to Show Content Conditionally

<!DOCTYPE html>

<html>

<head>

    <title>Conditional Example</title>

</head>

<body>

    <?php

    $hour = date(“H”);

    if ($hour < 12) {

        echo “<h2>Good Morning!</h2>”;

    } else {

        echo “<h2>Good Afternoon!</h2>”;

    }

    ?>

</body>

</html>

  • The if statement checks the current hour and displays a message accordingly.

6. Separating PHP Logic from HTML (Best Practice)

Instead of embedding all PHP code inside HTML, it’s a good practice to separate PHP logic from the HTML template.

Example: Using Separate PHP Logic File

index.php

<?php

$title = “PHP Best Practices”;

$greeting = “Hello, welcome to our website!”;

?>

<!DOCTYPE html>

<html>

<head>

    <title><?php echo $title; ?></title>

</head>

<body>

    <h1><?php echo $greeting; ?></h1>

</body>

</html>

  • This approach keeps PHP logic at the top and HTML below, making the code cleaner and easier to maintain.

7. Conclusion

Embedding PHP in HTML allows developers to create dynamic and interactive web pages by processing data, handling forms, and generating content dynamically. Best practices include separating PHP logic from HTML for better readability and maintainability.