📅  最后修改于: 2023-12-03 15:03:44.194000             🧑  作者: Mango
PHP是一种广泛使用的开源脚本语言,特别适合Web开发,可以嵌入HTML中使用,并与HTML代码混合编写。本教程将向您介绍PHP的基本知识和常用技巧,帮助您快速入门PHP编程。
<?php
// PHP代码在这里写
?>
<?php
$name = "John Doe";
$age = 25;
echo "姓名: " . $name . "<br>";
echo "年龄: " . $age . "<br>";
?>
<?php
$string = "This is a string.";
$number = 42;
$boolean = true;
$array = array(1, 2, 3);
$null = null;
?>
<?php
$a = 10;
$b = 5;
$sum = $a + $b;
$difference = $a - $b;
$product = $a * $b;
$quotient = $a / $b;
$remainder = $a % $b;
?>
<?php
$x = 5;
if ($x > 0) {
echo "x是正数";
} elseif ($x < 0) {
echo "x是负数";
} else {
echo "x是零";
}
for ($i = 1; $i <= 5; $i++) {
echo $i . "<br>";
}
$colors = array("Red", "Blue", "Green");
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
<?php
function greet($name) {
echo "Hello, " . $name . "!";
}
greet("Alice");
?>
<?php
$fruits = array("Apple", "Banana", "Orange");
echo $fruits[0]; // 输出:Apple
$person = array("name" => "John", "age" => 25);
echo $person["name"]; // 输出:John
?>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo "姓名: " . $name . "<br>";
echo "邮箱: " . $email . "<br>";
?>
<form method="post" action="">
<input type="text" name="name" placeholder="姓名" required>
<input type="email" name="email" placeholder="邮箱" required>
<input type="submit" value="提交">
</form>
<?php
// 读取文件
$file = fopen("example.txt", "r");
$content = fread($file, filesize("example.txt"));
fclose($file);
echo $content;
// 写入文件
$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);
?>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "mydb";
// 创建连接
$conn = new mysqli($servername, $username, $password, $database);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>
以上是本教程的一些主题,涵盖了PHP的基础知识和常用技巧。希望对你的PHP学习和开发有所帮助!