PHP (Hypertext Preprocessor) is a server-side scripting language widely used for web development. It can be embedded into HTML, collaborate with databases, and be used to create dynamic web pages. PHP has a wide range of functions and significance. This article will fundamentally understand the functions and significance of PHP and illustrate it through specific code examples.
1. The role and significance of PHP
2. Specific code examples
The following are some common PHP code examples, showing the role and significance of PHP in practical applications:
<!DOCTYPE html> <html> <head> <title>动态网页示例</title> </head> <body> <h1>欢迎使用动态网页</h1> <?php $name = "张三"; echo "<p>欢迎您,$name</p>"; ?> </body> </html>
In the above code, PHP dynamically inserts the value of the variable $name into HTML to achieve the effect of a dynamic web page.
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "myDB"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接数据库失败:" . $conn->connect_error); } $sql = "SELECT id, name, email FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>"; } } else { echo "0 个结果"; } $conn->close(); ?>
The above code connects to the database and queries the data in the user table, and then outputs the results to the web page.
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST['name']; $email = $_POST['email']; // 执行数据验证和处理 echo "提交成功!"; } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> 姓名:<input type="text" name="name"><br> 电子邮件:<input type="email" name="email"><br> <input type="submit" value="提交"> </form>
The above code shows a simple form. When the user submits the form, PHP will obtain the data in the form and process it. If the data is legal, "Submission successful" will be displayed.
In general, PHP, as a server-side scripting language, plays an important role in web development. Through functions such as dynamic web page generation, database operations, and form processing, PHP provides rich possibilities for website functions and interactions, and is an indispensable part of modern Web development.
The above is the detailed content of Understand the role and significance of PHP from the fundamentals. For more information, please follow other related articles on the PHP Chinese website!