Introduction to PHP

  1. What Is PHP?



  2. How PHP Works

  3. Browser ---> Request ---> Server (PHP engine) ---> Output HTML ---> Browser


  4. Your First PHP Script

  5. <?php
    echo "Hello, World!";
    ?>
    


  6. Embedding PHP in HTML

  7. <html>
    <body>
        <h1>Welcome</h1>
        <p>The time is: <?php echo date("H:i:s"); ?></p>
    </body>
    </html>
    


  8. PHP Variables

  9. <?php
    $name = "Junzhe";
    $age = 22;
    echo "Name: $name, Age: $age";
    ?>
    


  10. Basic Data Types

  11. Type Description
    String Text data
    Integer Whole numbers
    Float Decimal numbers
    Boolean true or false
    Array List or dictionary-like collections
    Object Instances of classes
    NULL No value


  12. PHP Arrays

  13. <?php
    $colors = ["red", "green", "blue"];
    $person = ["name" => "Alice", "age" => 30];
    ?>
    


  14. Control Structures

  15. <?php
    if ($age >= 18) {
        echo "Adult";
    } else {
        echo "Minor";
    }
    
    for ($i = 0; $i < 3; $i++) {
        echo $i;
    }
    ?>
    


  16. Functions

  17. <?php
    function greet($name) {
        return "Hello, $name!";
    }
    
    echo greet("Junzhe");
    ?>
    


  18. Working with Forms

  19. <form method="POST">
        Name: <input name="name">
        <input type="submit">
    </form>
    
    <?php
    if ($_POST) {
        echo "Hello " . $_POST["name"];
    }
    ?>
    


  20. Connecting to Databases

  21. <?php
    $pdo = new PDO("mysql:host=localhost;dbname=test", "user", "pass");
    
    $stmt = $pdo->query("SELECT * FROM users");
    foreach ($stmt as $row) {
        echo $row["name"];
    }
    ?>
    


  22. Why PHP Is Still Popular



  23. Popular Frameworks

  24. Framework Purpose
    Laravel Modern MVC, expressive syntax
    Symfony Enterprise-level framework
    CodeIgniter Lightweight, simple
    Yii Fast, component-based


  25. Summary

  26. Feature Description
    Server-side scripting PHP runs on the server to generate web content
    Embedded HTML Mix PHP directly with HTML
    Database support Works with MySQL, PostgreSQL, SQLite, etc.
    Variables start with $ Simplifies syntax
    Widely supported Available on almost all hosting services
    Large ecosystem WordPress, Laravel, Symfony, Drupal



Hello World in PHP

  1. Introduction



  2. Basic Hello World Example

  3. <?php
    echo "Hello, World!";
    ?>
    

    Hello, World!
    


  4. Embedding Hello World in HTML

  5. <!DOCTYPE html>
    <html>
    <body>
    
    <h1>My First PHP Page</h1>
    
    <p>
        <?php echo "Hello from PHP!"; ?>
    </p>
    
    </body>
    </html>
    


  6. Hello World Using print

  7. <?php
    print "Hello using print!";
    ?>
    


  8. Hello World on Command Line (CLI)

  9. php hello.php
    

    Hello, World!
    


  10. Ensuring PHP Is Installed

  11. php -v
    

    PHP 8.4.14 (cli) ...


  12. Running Hello World with a Built-in PHP Server

  13. php -S localhost:8000
    

    http://localhost:8000/hello.php
    


  14. Short Open Tags (Deprecated/Less Recommended)



  15. Summary

  16. Concept Description
    Hello World file echo "Hello, World!";
    Embedded PHP Use <?php ... ?> inside HTML
    CLI execution Run php hello.php in terminal
    Built-in server php -S localhost:8000
    echo vs print echo is slightly faster; both output text
    Short tags Not recommended; use full <?php tag



PHP Variables

  1. What Is a Variable in PHP?



  2. Basic Syntax and Naming Rules



  3. Dynamic Types: Changing the Value Type

    1. PHP allows the same variable to hold values of different types over time:
      <?php
      $value = 42;            // integer
      $value = "Hello";       // now string
      $value = 3.14;          // now float
      $value = true;          // now boolean
      ?>
      

    2. Common scalar types:

      • integer: whole numbers (1, -5)
      • float (double): decimal numbers (3.14)
      • string: text ("Hello", 'World')
      • boolean: true or false

    3. PHP automatically converts between types in many operations (type juggling):
      <?php
      $x = "10";          // string
      $y = 5;             // integer
      $sum = $x + $y;     // PHP converts "10" to int, result 15
      ?>
      

    4. You can explicitly cast types:
      <?php
      $number = "42";
      $intNumber = (int)$number;     // 42 as integer
      $floatNumber = (float)$number; // 42.0 as float
      ?>
      


  4. Strings and Variable Interpolation



  5. Arrays, Objects, and NULL



  6. Assignment by Value vs. Assignment by Reference



  7. Variable Scope: Local, Global, Static



  8. Superglobal Variables



  9. Variable Variables (Advanced)



  10. Checking and Debugging Variables




PHP Constants

  1. What Is a Constant in PHP?



  2. Defining Constants with define()



  3. Defining Constants with const



  4. Allowed Values for Constants



  5. Constant Naming Conventions



  6. Global Scope of Constants



  7. Checking Constants: defined() and constant()



  8. Magic Constants



  9. Class Constants



  10. Constants and Namespaces




PHP Comments

  1. Single-Line Comments



  2. Multi-Line Comments



  3. Inline Comments



  4. PHPDoc Comments (Documentation Comments)



PHP var_dump

  1. What Is var_dump?



  2. Basic Examples



  3. Dumping Arrays



  4. Dumping Objects



  5. Dumping NULL and Empty Values



  6. Dumping Multiple Values at Once



  7. Using var_dump in Browser vs CLI



  8. var_dump vs print_r vs var_export



  9. Using ob_start to Capture var_dump Output



  10. Formatting var_dump Output for Readability



  11. Common Use Cases for var_dump



PHP Data Types

  1. Overview of PHP Data Types



  2. Boolean Type (bool)



  3. Integer Type (int)



  4. Float Type (float or double)



  5. String Type (string)



  6. Array Type (array)



  7. Object Type (object)



  8. Callable Type (callable)



  9. Resource Type (resource)



  10. NULL Type (null)



  11. Type Juggling (Automatic Type Conversion)



  12. Type Casting



  13. Checking Types



  14. Best Practices for Using PHP Data Types



PHP Control Flow

  1. Conditional Statements: if, elseif, else



  2. Ternary Operator (?:)



  3. switch Statement



  4. match Expression (PHP 8+)



  5. while Loop



  6. do...while Loop



  7. for Loop



  8. foreach Loop



  9. break and continue



  10. return Statement



  11. Exception Handling (try, catch, finally)



  12. Alternative Syntax for Control Structures (Useful in HTML)



PHP Functions

  1. What Is a Function in PHP?



  2. Defining a Function



  3. Function Parameters



  4. Default Parameter Values



  5. Type Declarations for Parameters and Return Types



  6. Returning Values



  7. Variable-Length Argument Lists



  8. Anonymous Functions (Closures)



  9. Arrow Functions (PHP 7.4+)



  10. Named Arguments (PHP 8+)



  11. Recursive Functions



  12. Function Existence and Dynamic Calls



  13. Global Variables Inside Functions



  14. Static Variables Inside Functions



PHP Advanced Functions

  1. Higher-Order Functions (Functions That Take Functions)



  2. Closures with use



  3. Binding Closures to Objects



  4. Generators (yield)



  5. Anonymous Recursion



  6. Currying and Partial Application



  7. Callable Types and Callbacks



  8. Reflection API for Functions



  9. Generators with Keys and yield =>



  10. Function Factories



  11. Decorators / Function Wrappers



  12. Strict Types and Advanced Typing (PHP 7+)



PHP Arrays

  1. What Is an Array in PHP?



  2. Creating Arrays



  3. Array Keys and Automatic Indexing



  4. Accessing Array Elements



  5. Modifying Arrays



  6. Iterating Arrays



  7. Multidimensional Arrays



  8. Array Functions (Most Common)



  9. Array Spread Operator (PHP 7.4+)



  10. Sorting Arrays



  11. Array Destructuring (PHP 7.1+)



PHP Advanced Array Operations

  1. Array Mapping (array_map)



  2. Filtering Arrays (array_filter)



  3. Reducing Arrays (array_reduce)



  4. Array Merging and Combining



  5. Array Slicing (array_slice)



  6. Array Splicing (array_splice)



  7. Advanced Searching



  8. Set Operations



  9. Chunking and Splitting Arrays



  10. Array Destructuring (Advanced)



  11. Array Pointers



  12. Functional Programming with Arrays



Organizing PHP Files

  1. Typical Small Project Structure



  2. Using a Front Controller (index.php)



  3. Separating Configuration Files



  4. Using require, include, and autoload



  5. Organizing with Namespaces and PSR-4



  6. Separating Views (Templates) from Logic



  7. Organizing Environment-Specific Settings



  8. Splitting Logic into Domains (Controllers, Services, Models)



  9. Organizing Reusable Functions and Helpers



  10. Organizing Tests (If You Use PHPUnit)



  11. Organizing for Deployment



PHP State Management

  1. What Is “State” in PHP and HTTP?



  2. Per-Request Data: $_GET and $_POST



  3. Client-Side State: Cookies ($_COOKIE)



  4. Server-Side State: Sessions ($_SESSION)



  5. State via URL: Path and Query Design



  6. Persistent State: Database and Storage



  7. Managing Authentication State



  8. Flash Messages (One-Time State)



  9. State Management in APIs (Stateless vs Session-Based)



  10. Security Considerations in State Management



PHP Processing Forms

  1. Overview of Form Processing in PHP



  2. Creating a Basic Form



  3. Accessing Form Data in PHP



  4. Detecting Form Submission



  5. Validating Form Inputs



  6. Sanitizing and Escaping Input



  7. Displaying Validation Errors



  8. Preserving Form Values After Submission



  9. Processing Checkbox, Radio, and Select Fields



  10. Redirect After Processing (Post/Redirect/Get)



  11. Handling File Uploads



  12. Security Considerations



PHP Working with Files

  1. Checking File Existence and Metadata



  2. Reading Files



  3. Writing Files



  4. Working with Directories



  5. File Uploads via Forms



  6. Handling File Upload Errors



  7. Reading and Writing CSV Files



  8. Working with JSON Files



  9. File Locking for Concurrency



  10. File Downloads



  11. Security Considerations



PHP Date and Time

  1. Overview of Date and Time Handling in PHP



  2. Getting the Current Date and Time



  3. DateTime Class (Recommended)



  4. Timezones



  5. Date Arithmetic (Adding / Subtracting Time)



  6. Calculating Date Differences



  7. Parsing Dates from Strings (strtotime())



  8. Working with Unix Timestamps



  9. Date Formatting Examples



  10. Immutable Dates (DateTimeImmutable)



  11. Working with Time Intervals



  12. Generating Timestamps for Logging



PHP Namespaces

  1. What Are Namespaces in PHP?



  2. Declaring a Namespace



  3. Using Namespaced Classes



  4. Namespace Aliases



  5. Grouping Multiple Imports



  6. Global Namespace



  7. Defining Functions and Constants in Namespaces



  8. Subnamespaces and Project Organization



  9. How Namespaces Work with Autoloading (PSR-4)



  10. Nested Namespaces in One File



  11. Fully Qualified Names vs Relative Names



  12. Common Errors and Pitfalls