Backend Development 5 min read

Step-by-Step Guide to Generating Dynamic Web Pages with PHP

This article explains how to set up a PHP development environment, create .php files, embed PHP within HTML, handle form input, and interact with a MySQL database to produce dynamic web pages, providing clear code examples for each step.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Step-by-Step Guide to Generating Dynamic Web Pages with PHP

PHP is a widely used server‑side scripting language that can be combined with HTML to create dynamic, personalized web pages. This guide walks through the process of building such pages from scratch.

Step 1: Set up the PHP development environment – Install a web server (Apache) and the PHP interpreter, typically using bundles like WAMP, MAMP, or XAMPP that also include MySQL.

Step 2: Create a PHP file – Open a text editor and save a file with the .php extension (e.g., index.php ).

Step 3: Combine PHP with HTML – PHP code can be embedded directly inside HTML using <?php … ?> tags. A simple example is shown below:

<!DOCTYPE html>
<html>
<body>

<h1>欢迎来到我的网站!</h1>

<?php
    echo "当前时间是:" . date("Y-m-d H:i:s");
?>

</body>
</html>

When this file is opened in a browser, it displays a heading and the current server time.

Step 4: Generate dynamic content based on user input – By retrieving form data via the $_POST superglobal, PHP can produce personalized output. Example:

<!DOCTYPE html>
<html>
<body>

<h1>欢迎来到我的网站!</h1>

<?php
    // Get data submitted from the form
    $name = $_POST['name'];
    $age  = $_POST['age'];

    // Output a greeting using the submitted values
    echo "你好," . $name . "!你今年" . $age . "岁了。";
?>
姓名:<input type="text" name="name"><br>
    年龄:<input type="text" name="age"><br>
    <input type="submit" value="提交">
</body>
</html>

This script displays a form, captures the user's name and age, and then echoes a customized greeting.

Step 5: Interact with a MySQL database – PHP can connect to MySQL, run queries, and display results. A basic example:

<?php
    // Connect to the database
    $conn = mysqli_connect("localhost", "username", "password", "database");

    // Verify the connection
    if (! $conn) {
        die("数据库连接失败:" . mysqli_connect_error());
    }

    // Query data
    $sql = "SELECT * FROM users";
    $result = mysqli_query($conn, $sql);

    // Output the query results
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "用户名:" . $row["username"] . "
";
        }
    } else {
        echo "没有查询到数据。";
    }

    // Close the connection
    mysqli_close($conn);
?>

The script connects to a MySQL server, retrieves all rows from the users table, and prints each username, handling the case where no data is found.

By following these steps—setting up the environment, creating PHP files, mixing PHP with HTML, handling form input, and accessing a database—you can start building dynamic web pages with PHP.

backendMySQLweb developmentphptutorialdynamic pages
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.