PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques
Embedding PHP in HTML allows you to create dynamic web pages by inserting PHP code within HTML code. PHP (Hypertext Preprocessor) is a server-side scripting language designed for web development, but it can also be used as a general-purpose programming language. Here’s an in-depth look at how PHP can be embedded in HTML:

Basic Syntax

PHP code can be embedded in HTML using the PHP tags: ‘<?php ... ?>‘. Any code written within these tags will be executed on the server before the HTML is sent to the client.

Example:

				
					<!DOCTYPE html>
<html>
<head>
    <title>My PHP Page</title>
</head>
<body>
    <h1>Welcome to my website</h1>
    <?php
    echo "<p>This is a paragraph generated by PHP!</p>";
    ?>
</body>
</html>

				
			

In this example, the PHP code within <?php ... ?> tags outputs a paragraph element.

Using Short Tags

PHP also supports short tags <? ... ?> and <?= ... ?> (the latter is used for outputting values directly).

Example:

				
					<!DOCTYPE html>
<html>
<head>
    <title>Short Tags Example</title>
</head>
<body>
    <h1>Using Short Tags</h1>
    <? echo "<p>This is a paragraph generated by PHP using short tags!</p>"; ?>
    <?= "<p>This is another way to echo content using <?= ... ?>.</p>" ?>
</body>
</html>

				
			

Mixing HTML and PHP

You can switch between HTML and PHP as needed. This allows you to seamlessly integrate server-side logic with your HTML.

Example:

				
					<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Content Example</title>
</head>
<body>
    <h1>Dynamic Content</h1>
    <?php
    $hour = date("H");

    if ($hour < 12) {
        echo "<p>Good morning!</p>";
    } elseif ($hour < 18) {
        echo "<p>Good afternoon!</p>";
    } else {
        echo "<p>Good evening!</p>";
    }
    ?>
</body>
</html>

				
			

In this example, PHP is used to generate a different greeting based on the time of day.

Handling Forms with PHP

One of the most common uses of PHP is handling form submissions. Here’s a simple example:

Example:

				
					<!DOCTYPE html>
<html>
<head>
    <title>Form Handling with PHP</title>
</head>
<body>
    <h1>Contact Us</h1>
    <form method="post" action="">
        Name: <input type="text" name="name"><br>
        Email: <input type="email" name="email"><br>
        <input type="submit">
    </form>

    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = htmlspecialchars($_POST['name']);
        $email = htmlspecialchars($_POST['email']);
        echo "<p>Thank you, $name. We will contact you at $email.</p>";
    }
    ?>
</body>
</html>

				
			

In this example, the form data is processed by PHP, which then outputs a message using the submitted data.

Including Files

PHP allows you to include other PHP files within your code using ‘include or require‘. This is useful for separating concerns and reusing code.

Example:

header.php

				
					<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <header>
        <h1>My Website Header</h1>
    </header>

				
			

footer.php

				
					    <footer>
        <p>Footer content here</p>
    </footer>
</body>
</html>

				
			

index.php

				
					<?php include 'header.php'; ?>
    <main>
        <h2>Welcome to my website</h2>
        <p>This is the main content.</p>
    </main>
<?php include 'footer.php'; ?>

				
			

In this example, ‘header.php‘ and ‘footer.php‘ are included in ‘index.php‘, which helps maintain a consistent layout.

Using PHP for Dynamic Content

PHP can be used to interact with databases, manage sessions, and handle cookies, allowing for the creation of fully dynamic websites.

Database Example (MySQL):

				
					<!DOCTYPE html>
<html>
<head>
    <title>Database Example</title>
</head>
<body>
    <h1>User List</h1>
    <?php
    $conn = new mysqli("localhost", "username", "password", "database");

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT id, name, email FROM users";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<ul>";
        while($row = $result->fetch_assoc()) {
            echo "<li>" . $row["name"] . " (" . $row["email"] . ")</li>";
        }
        echo "</ul>";
    } else {
        echo "<p>No users found.</p>";
    }

    $conn->close();
    ?>
</body>
</html>

				
			

In this example, PHP connects to a MySQL database, retrieves user data, and dynamically generates an HTML list of users.

Scroll to Top