本文目录导读:

我来帮你讲解PHP页面设计的方法和基本架构。
基础PHP页面结构
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">PHP页面示例</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
// PHP代码区
$title = "欢迎来到PHP页面";
echo "<h1>" . $title . "</h1>";
// 获取当前时间
$current_time = date('Y-m-d H:i:s');
echo "<p>当前时间:{$current_time}</p>";
?>
</body>
</html>
常用页面设计模式
1 混合模式(推荐)
<!DOCTYPE html>
<html>
<head><?php echo $page_title; ?></title>
</head>
<body>
<header>
<h1><?= $site_name ?></h1>
</header>
<main>
<?php if ($user_logged_in): ?>
<p>欢迎回来,<?= htmlspecialchars($username) ?>!</p>
<?php else: ?>
<p>请登录</p>
<?php endif; ?>
</main>
</body>
</html>
2 模板分离模式
template/header.php
<!DOCTYPE html>
<html>
<head><?php echo $title; ?></title>
</head>
<body>
<header>
<nav>
<a href="index.php">首页</a>
<a href="about.php">lt;/a>
</nav>
</header>
template/footer.php
<footer>
<p>© <?php echo date('Y'); ?> 我的网站</p>
</footer>
</body>
</html>
page.php
<?php
$title = "关于我们";
include 'template/header.php';
?>
<main>
<h1>关于我们</h1>
<p>这是关于页面的内容...</p>
</main>
<?php include 'template/footer.php'; ?>
数据展示设计
<?php
// 获取数据(示例)
$users = [
['name' => '张三', 'email' => 'zhangsan@example.com'],
['name' => '李四', 'email' => 'lisi@example.com'],
];
?>
<table class="data-table">
<thead>
<tr>
<th>姓名</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= htmlspecialchars($user['name']) ?></td>
<td><?= htmlspecialchars($user['email']) ?></td>
<td>
<a href="edit.php?id=<?= $user['id'] ?>">编辑</a>
<a href="delete.php?id=<?= $user['id'] ?>" onclick="return confirm('确定删除?')">删除</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
表单处理设计
<?php
$errors = [];
$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
// 验证
if (empty($name)) $errors[] = '姓名不能为空';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = '邮箱格式不正确';
if (empty($errors)) {
// 处理数据
$success = true;
$message = "注册成功!";
}
}
?>
<form method="post" action="">
<?php if (!empty($errors)): ?>
<div class="error-messages">
<?php foreach ($errors as $error): ?>
<p class="error"><?= htmlspecialchars($error) ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success-message"><?= $message ?></div>
<?php endif; ?>
<label for="name">姓名:</label>
<input type="text" id="name" name="name"
value="<?= htmlspecialchars($name ?? '') ?>" required>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email"
value="<?= htmlspecialchars($email ?? '') ?>" required>
<button type="submit">提交</button>
</form>
分页设计
<?php
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 10;
$offset = ($page - 1) * $per_page;
// 获取总数(示例)
$total = 100;
$total_pages = ceil($total / $per_page);
// 分页导航
function pagination($current_page, $total_pages) {
$html = '<nav class="pagination"><ul>';
for ($i = 1; $i <= $total_pages; $i++) {
$active = $i == $current_page ? ' class="active"' : '';
$html .= "<li{$active}><a href='?page={$i}'>{$i}</a></li>";
}
$html .= '</ul></nav>';
return $html;
}
?>
<div class="content">
<!-- 显示数据 -->
</div>
<?= pagination($page, $total_pages) ?>
最佳实践建议
-
安全性:始终使用
htmlspecialchars()输出用户数据 -
可维护性:使用模板分离,避免PHP和HTML混杂过多
-
性能优化:
- 使用缓存机制
- 减少数据库查询
- 压缩CSS/JS文件
-
响应式设计:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
-
使用现代框架:考虑使用 Laravel、Symfony 等框架
-
错误处理:
<?php error_reporting(E_ALL); ini_set('display_errors', 0); // 生产环境关闭 ini_set('log_errors', 1); ?>
这些是PHP页面设计的基础方法,根据你的具体需求,可以选择不同的设计模式,如果需要更具体的示例,请告诉我你的具体需求。