php基础知识
PHP(Hypertext Preprocessor)是一种广泛用于服务器端的脚本语言,特别适合Web开发。以下是一些PHP的基础知识:<?php ... ?>
标签来包含PHP代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Example</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
$
符号开头,后跟变量名。变量名是区分大小写的。
<?php
$greeting = "Hello, World!";
echo $greeting;
?>
<?php
$string = "Hello, World!";
$integer = 42;
$float = 3.14;
$boolean = true;
$array = array("apple", "banana", "cherry");
$null = NULL;
?>
<?php
// 索引数组
$fruits = array("apple", "banana", "cherry");
echo $fruits[0]; // 输出 "apple"
// 关联数组
$ages = array("Alice" => 25, "Bob" => 30, "Charlie" => 35);
echo $ages["Alice"]; // 输出 25
?>
if
、else
、elseif
进行条件判断。
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
for
、while
、do-while
和 foreach
。
<?php
// for 循环
for ($i = 0; $i < 5; $i++) {
echo $i;
}
// while 循环
$i = 0;
while ($i < 5) {
echo $i;
$i++;
}
// do-while 循环
$i = 0;
do {
echo $i;
$i++;
} while ($i < 5);
// foreach 循环
$fruits = array("apple", "banana", "cherry");
foreach ($fruits as $fruit) {
echo $fruit;
}
?>
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice");
?>
<?php
// 获取GET请求参数
$name = $_GET['name'];
echo "Hello, " . $name;
// 获取POST请求参数
$age = $_POST['age'];
echo "Your age is " . $age;
// 获取服务器信息
echo $_SERVER['HTTP_USER_AGENT'];
?>
<?php
// 写入文件
$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);
// 读取文件
$file = fopen("example.txt", "r");
echo fread($file, filesize("example.txt"));
fclose($file);
?>
mysqli
扩展mysqli
扩展提供了面向对象和过程化两种方式来进行数据库操作。以下是面向对象的示例:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 插入数据
$sql = "INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// 查询数据
$sql = "SELECT id, username, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出每行数据
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["username"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
// 关闭连接
$conn->close();
?>
PDO
扩展PDO
(PHP Data Objects)提供了一种统一的数据库访问接口,支持多种数据库系统。
<?php
$dsn = 'mysql:host=localhost;dbname=database';
$username = 'username';
$password = 'password';
try {
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 插入数据
$stmt = $pdo->prepare("INSERT INTO users (username, email) VALUES (:username, :email)");
$stmt->execute(['username' => 'john_doe', 'email' => 'john@example.com']);
echo "New record created successfully";
// 查询数据
$stmt = $pdo->query("SELECT id, username, email FROM users");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "id: " . $row['id'] . " - Name: " . $row['username'] . " - Email: " . $row['email'] . "<br>";
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
$_GET
和 $_POST
超全局数组来获取表单提交的数据。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Example</title>
</head>
<body>
<form action="process_form.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<?php
// process_form.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
echo "Name: " . $name . "<br>";
echo "Email: " . $email . "<br>";
}
?>
$_SESSION
超全局数组来管理会话数据。
<?php
// 开启会话
session_start();
// 设置会话变量
$_SESSION["username"] = "john_doe";
echo "Session variables are set.";
// 获取会话变量
echo "Username: " . $_SESSION["username"];
?>
<?php
// 自定义错误处理函数
function customError($errno, $errstr) {
echo "Error: [$errno] $errstr<br>";
}
// 设置错误处理函数
set_error_handler("customError");
// 触发错误
echo($test);
?>