Home >Backend Development >PHP Tutorial >Learn more about PHP: from beginner to proficient
In-depth understanding of PHP: from entry to proficiency
Introduction:
PHP is a server-side scripting language widely used in web development. It is simple and easy to learn. It is suitable for beginners and also provides rich functions and scalability to meet complex development needs. This article will take you step by step to understand all aspects of PHP through specific code examples, from beginner to proficient.
1. Basic knowledge
Variable declaration and output
$name = "John"; $age = 25; echo "My name is " . $name . " and I am " . $age . " years old.";
Conditional statements
$score = 90; if ($score >= 60) { echo "You passed the exam!"; } else { echo "You failed the exam."; }
Loop statement
for ($i = 0; $i < 5; $i++) { echo $i; }
Definition and call of function
function sayHello($name) { echo "Hello, " . $name . "!"; } sayHello("Tom");
2. Arrays and strings
Array declaration and traversal
$fruits = ["apple", "banana", "orange"]; foreach ($fruits as $fruit) { echo $fruit; }
String operation
$str = "Hello, PHP!"; echo strlen($str); // 输出:12 echo strtoupper($str); // 输出:HELLO, PHP! echo substr($str, 7); // 输出:PHP!
3. Database operation
Connect to database
$servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
Query data
$sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { echo "Name: " . $row['name'] . ", Age: " . $row['age']; } } else { echo "No results found."; }
Insert data
$sql = "INSERT INTO users (name, age) VALUES ('John', 25)"; if (mysqli_query($conn, $sql)) { echo "Data inserted successfully."; } else { echo "Error: " . mysqli_error($conn); }
4. Object-oriented programming
Definition and instantiation of classes
class Car { public $color; public function drive() { echo "Driving..."; } } $car = new Car(); $car->color = "red"; $car->drive();
Inheritance and polymorphism
class SportsCar extends Car { public function drive() { echo "Driving at high speed!"; } } $sportsCar = new SportsCar(); $sportsCar->color = "blue"; $sportsCar->drive();
5. Common applications
File upload
<form action="upload.php" method="POST" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form>
$targetDir = "uploads/"; $targetFile = $targetDir . basename($_FILES["file"]["name"]); if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) { echo "File uploaded successfully."; } else { echo "Error uploading file."; }
User registration and login
$hashedPassword = password_hash($password, PASSWORD_DEFAULT); $sql = "INSERT INTO users (username, password) VALUES ('$username', '$hashedPassword')"; if (mysqli_query($conn, $sql)) { echo "Registration successful. You can now login."; } else { echo "Error: " . mysqli_error($conn); }
$sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_assoc($result); if (password_verify($password, $row['password'])) { echo "Login successful."; } else { echo "Password incorrect."; } } else { echo "Username not found."; }
Conclusion:
Through the introduction and example code of this article, I hope that readers can start from the basic knowledge of PHP and gradually understand all aspects of PHP, so as to master the advanced application skills of PHP and improve their development capabilities. At the same time, readers are also welcome to explore more interesting and innovative PHP applications in practice.
The above is the detailed content of Learn more about PHP: from beginner to proficient. For more information, please follow other related articles on the PHP Chinese website!