Home > Backend Development > PHP Tutorial > Learn more about PHP: from beginner to proficient

Learn more about PHP: from beginner to proficient

王林
Release: 2023-12-19 15:08:01
Original
1314 people have browsed it

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

  1. Variable declaration and output

    $name = "John";
    $age = 25;
    echo "My name is " . $name . " and I am " . $age . " years old.";
    Copy after login
  2. Conditional statements

    $score = 90;
    if ($score >= 60) {
     echo "You passed the exam!";
    } else {
     echo "You failed the exam.";
    }
    Copy after login
  3. Loop statement

    for ($i = 0; $i < 5; $i++) {
     echo $i;
    }
    Copy after login
  4. Definition and call of function

    function sayHello($name) {
     echo "Hello, " . $name . "!";
    }
    sayHello("Tom");
    Copy after login

2. Arrays and strings

  1. Array declaration and traversal

    $fruits = ["apple", "banana", "orange"];
    foreach ($fruits as $fruit) {
     echo $fruit;
    }
    Copy after login
  2. String operation

    $str = "Hello, PHP!";
    echo strlen($str);  // 输出:12
    echo strtoupper($str);  // 输出:HELLO, PHP!
    echo substr($str, 7);  // 输出:PHP!
    Copy after login

3. Database operation

  1. 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());
    }
    Copy after login
  2. 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.";
    }
    Copy after login
  3. 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);
    }
    Copy after login

4. Object-oriented programming

  1. Definition and instantiation of classes

    class Car {
     public $color;
     public function drive() {
         echo "Driving...";
     }
    }
    $car = new Car();
    $car->color = "red";
    $car->drive();
    Copy after login
  2. Inheritance and polymorphism

    class SportsCar extends Car {
     public function drive() {
         echo "Driving at high speed!";
     }
    }
    $sportsCar = new SportsCar();
    $sportsCar->color = "blue";
    $sportsCar->drive();
    Copy after login

5. Common applications

  1. File upload

    <form action="upload.php" method="POST" enctype="multipart/form-data">
     <input type="file" name="file">
     <input type="submit" value="Upload">
    </form>
    Copy after login
    $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.";
    }
    Copy after login
  2. 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);
    }
    Copy after login
    $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.";
    }
    Copy after login

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!

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