Home  >  Article  >  Backend Development  >  Dynamic Websites Made Easy: Learning the Basics of PHP

Dynamic Websites Made Easy: Learning the Basics of PHP

WBOY
WBOYOriginal
2024-10-09 20:03:31776browse

PHP is a general-purpose scripting language specifically designed for web development to create websites that dynamically respond to user input. Its basic syntax includes PHP tags, echo statements, variables (declared with the $ symbol), data types (int, float, etc.), conditional statements (such as if statements), loops (for and while loops), and the ability to connect to a database (such as MySQL). A simple message board practical case demonstrates the practical application of using PHP for data processing and page interaction.

Dynamic Websites Made Easy: Learning the Basics of PHP

Introduction to Dynamic Website Development: PHP Basics

PHP (Hypertext Preprocessor) is a general-purpose scripting language designed specifically for web development. By using PHP we can create dynamic websites that respond to user input.

Basic syntax of PHP

A simple PHP script looks like this:

<?php

  // 这是 PHP 代码
  echo "你好,世界!";

?>
  • The PHP code is contained within the 5c8da1d8b4c5a9634193c26feb258147 tags . The
  • echo statement outputs the string "Hello, world!" to a web browser.

Variables

Variables are used to store data. They can be declared with the $ symbol:

$name = "John Doe";

Now, we can use the variable $name to access its value.

Data types

PHP supports a variety of data types, including:

int (整数)
float (小数)
string (字符串)
boolean (布尔值)
array (数组)

Conditional statements

Conditional statements are used to control the execution of code. The most common conditional statement is the if statement:

if ($age >= 18) {
  echo "成年";
} else {
  echo "未成年";
}

Loop

Loop is used to repeatedly execute a block of code. There are two main types of loops:

  • for Loop: Used to execute a loop a known number of times.
  • while Loop: used to execute loops whose conditions are true.

Database Connections

PHP can connect to databases such as MySQL, PostgreSQL, and SQL Server. This allows us to read and write data from the database.

// 连接到 MySQL 数据库
$conn = mysqli_connect("localhost", "root", "password", "test");

// 执行查询
$result = mysqli_query($conn, "SELECT * FROM users");

// 获取结果并显示
while ($row = mysqli_fetch_array($result)) {
  echo $row["name"];
}

Practical case: a simple message board

We create a simple message board that allows users to enter and view messages.

<!-- index.html -->
<form action="save.php" method="POST">
  <input type="text" name="message">
  <input type="submit" value="发表">
</form>
<!-- save.php -->
<?php

// 获取表单数据
$message = $_POST["message"];

// 连接到数据库
$conn = mysqli_connect("localhost", "root", "password", "test");

// 插入留言
$query = "INSERT INTO messages (message) VALUES ('$message')";
mysqli_query($conn, $query);

// 重定向到列表页面
header("Location: list.php");

?>
<!-- list.php -->
<?php

// 连接到数据库
$conn = mysqli_connect("localhost", "root", "password", "test");

// 获取留言
$query = "SELECT * FROM messages";
$result = mysqli_query($conn, $query);

// 显示留言
while ($row = mysqli_fetch_array($result)) {
  echo $row["message"];
  echo "<br>";
}

?>

The above is the detailed content of Dynamic Websites Made Easy: Learning the Basics of PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn