本文目录导读:

我来为你提供一份系统的PHP练习指南,从基础到进阶,帮助你循序渐进地掌握PHP。
🎯 基础练习(第1-2周)
语法基础练习
<?php
// 练习1:变量和数据类型
$name = "张三";
$age = 25;
$height = 1.75;
$isStudent = true;
echo "姓名:$name,年龄:$age,身高:$height米";
var_dump($isStudent); // 显示变量类型和值
// 练习2:字符串操作
$str = "Hello, PHP!";
echo strlen($str); // 字符串长度
echo strtoupper($str); // 转大写
echo str_replace("PHP", "World", $str); // 替换
?>
流程控制练习
<?php
// 练习:判断成绩等级
$score = 85;
if ($score >= 90) {
echo "优秀";
} elseif ($score >= 80) {
echo "良好";
} elseif ($score >= 60) {
echo "及格";
} else {
echo "不及格";
}
// 循环输出九九乘法表
for ($i = 1; $i <= 9; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo "$j×$i=" . ($i*$j) . " ";
}
echo "<br>";
}
?>
📚 中级练习(第3-4周)
函数和数组练习
<?php
// 练习:自定义函数
function calculateArea($radius) {
return pi() * $radius * $radius;
}
// 数组操作练习
$students = [
['name' => '小明', 'score' => 85],
['name' => '小红', 'score' => 92],
['name' => '小刚', 'score' => 78]
];
// 数组排序
usort($students, function($a, $b) {
return $b['score'] - $a['score'];
});
// 数组遍历和聚合
$totalScore = array_sum(array_column($students, 'score'));
$average = $totalScore / count($students);
?>
文件操作练习
<?php
// 练习:文件读写
$file = fopen("test.txt", "w");
fwrite($file, "Hello, file!");
fclose($file);
// 读取文件
$content = file_get_contents("test.txt");
echo $content;
// CSV文件处理
$csvData = array_map('str_getcsv', file('data.csv'));
foreach ($csvData as $row) {
echo $row[0] . " - " . $row[1] . "<br>";
}
?>
🚀 进阶练习(第5-8周)
面向对象编程练习
<?php
// 练习:创建一个简单的类
class User {
private $name;
private $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getName() {
return $this->name;
}
public function sendEmail($message) {
// 模拟发送邮件
return "向 {$this->email} 发送:{$message}";
}
// 魔术方法
public function __toString() {
return "用户:{$this->name}";
}
}
// 练习:继承和多态
class Admin extends User {
private $permissions = [];
public function addPermission($perm) {
$this->permissions[] = $perm;
}
}
?>
MySQL数据库操作练习
<?php
// PDO连接数据库
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "password");
// 增删改查练习
// 插入数据
$stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->execute(['张三', 'zhangsan@example.com']);
// 查询数据
$stmt = $pdo->query("SELECT * FROM users WHERE age > 18");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 更新数据
$stmt = $pdo->prepare("UPDATE users SET name = ? WHERE id = ?");
$stmt->execute(['李四', 1]);
// 删除数据
$stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
$stmt->execute([5]);
?>
💻 实战项目练习
创建简易博客系统
// blog.php
<?php
session_start();
// 简单的博客系统
class Blog {
private $posts = [];
public function createPost($title, $content) {
$this->posts[] = [
'title' => $title,
'content' => $content,
'date' => date('Y-m-d H:i:s')
];
}
public function displayPosts() {
foreach ($this->posts as $post) {
echo "<h2>{$post['title']}</h2>";
echo "<p>{$post['content']}</p>";
echo "<small>{$post['date']}</small>";
}
}
}
$blog = new Blog();
$blog->createPost("我的第一篇博客", "这是内容...");
$blog->displayPosts();
?>
用户登录系统练习
<?php
session_start();
// 用户认证系统
class Auth {
private $db;
public function __construct($pdo) {
$this->db = $pdo;
}
public function login($username, $password) {
$stmt = $this->db->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
return true;
}
return false;
}
public function logout() {
session_destroy();
}
public function isLoggedIn() {
return isset($_SESSION['user_id']);
}
}
?>
📝 练习建议
每日练习计划
- 每天至少1小时编码练习
- 每周完成2-3个小项目
- 月末完成一个综合性项目
练习资源推荐
<!-- 在线练习平台 --> - PHPBolt(phpbolt.com) - W3Schools PHP练习 - PHP Exercise(php-exercise.net) <!-- 本地练习环境 --> - 使用XAMPP/WAMP搭建本地环境 - 安装PHPStorm/VSCode进行编码 - 配置Git进行版本控制
常见练习题目
<?php
// 练习题1:删除数组中重复元素
$array = [1, 2, 2, 3, 4, 4, 5];
$unique = array_unique($array);
// 练习题2:字符串反转
function reverseString($str) {
return strrev($str);
}
// 练习题3:计算两个日期差
$date1 = new DateTime('2024-01-01');
$date2 = new DateTime('2024-12-31');
$difference = $date1->diff($date2);
echo $difference->days . " days";
?>
🎓 学习路径建议
| 阶段 | 时间 | 目标 | |
|---|---|---|---|
| 入门 | 2周 | 语法基础 | 能写简单脚本 |
| 基础 | 4周 | 函数、数组、文件 | 完成小项目 |
| 进阶 | 6周 | OOP、数据库 | 开发完整应用 |
| 实战 | 8周+ | MVC、框架 | 熟练开发 |
核心要点:
- 动手实践最重要,不要只看视频
- 逐步构建自己的代码库
- 参与开源项目
- 阅读别人的代码并改进
有任何具体问题,欢迎随时提问!祝你PHP学习顺利!🎉