📅  最后修改于: 2023-12-03 15:35:18.819000             🧑  作者: Mango
PHP is a widely used server-side scripting language that is especially suited for web development. It can be embedded into HTML, and can be used to create dynamic web pages, perform database operations, process user input, generate PDF documents, and much more.
Easy to Learn: PHP is a beginner-friendly language with a simple syntax and easy-to-understand principles.
Cross-Platform Compatibility: PHP is cross-platform meaning it can be run on different operating systems like Windows, Linux, and macOS.
Powerful and Versatile: PHP has a wide range of libraries, frameworks, and extensions that allow developers to build robust web applications with ease.
Open-Source: PHP is an open-source language, meaning it is free to use, share, and modify.
Here's a simple "Hello, World" example in PHP:
<?php
echo "Hello, World!";
?>
In this example, the echo
statement is used to print the message "Hello, World!" to the browser. You can save this code to a file with a .php
extension, and then access it via a web browser.
PHP can be used to create dynamic web pages that interact with databases:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
In this example, PHP is used to connect to a MySQL database and retrieve data from it. The mysqli
extension is used to interact with MySQL databases, and the while
loop is used to print out each row of data returned by the query.
Overall, PHP is a powerful and versatile language that is widely used in web development. Whether you're building a small website or a complex web application, PHP has the tools you need to get the job done.