Dynamic Websites Made Easy: Learning the Basics of PHP

WBOY
Release: 2024-10-09 20:03:31
Original
773 people have browsed it

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 "你好,世界!";

?>
Copy after login
  • The PHP code is contained within the <?php and ?> 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";
Copy after login

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 (数组)
Copy after login

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 "未成年";
}
Copy after login

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"];
}
Copy after login

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>
Copy after login
<!-- 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");

?>
Copy after login
<!-- 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>";
}

?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template