PHP for Beginners

Learn PHP from your first script to handling real form data safely.

  • Beginner
  • 13 lessons
  • 8 hours

Start the course

PHP runs a large share of the web, from personal blogs to WordPress, Laravel and Wikipedia. This course teaches modern PHP (version 8.1 and newer) from zero.

Each lesson is short, focuses on one idea, and ends with code you can run. By the end you will build a small form handler that validates input and avoids the security mistakes beginners usually make.

Type the examples yourself rather than copying them. Small typos and the error messages they cause are part of learning.

Curriculum

  1. 1 Getting started

    What PHP is, how to install it, and the shape of a PHP file.

    1. What is PHP? 3 min
    2. Installing PHP and running your first script 3 min
    3. PHP syntax: tags, statements, comments and echo 4 min
  2. 2 The core language

    Variables, types, operators, strings and control flow.

    1. Variables and data types 4 min
    2. Operators and expressions 4 min
    3. Working with strings 4 min
    4. Making decisions: if, else, switch and match 4 min
    5. Loops: while, for and foreach 4 min
  3. 3 Working with data

    Group values in arrays and package logic in functions.

    1. Arrays 4 min
    2. Functions 5 min
  4. 4 PHP on the web

    Handle form input safely, organise files and fix errors.

    1. Handling forms safely 5 min
    2. Organising code with include and require 5 min
    3. Errors, exceptions and debugging 5 min

Test yourself: PHP for Beginners: final quiz

10 questions. Pass mark 70%.

  1. Question 1 of 10 Which command starts PHP’s built-in web server on port 8000?
  2. Question 2 of 10 What does this print?
    PHP
    $a = '5';
    $b = 5;
    var_dump($a === $b);
  3. Question 3 of 10 Which of these are valid variable names in PHP?

    Select all that apply.

  4. Question 4 of 10 What is the output?
    PHP
    $name = 'Sam';
    echo "Hello, {$name}!";
    echo 'Hello, {$name}!';
  5. Question 5 of 10 Which expression returns ‘pass’ when $score is 70 or more, and ‘fail’ otherwise, without any if statement?
  6. Question 6 of 10 How many times does this loop print a line?
    PHP
    for ($i = 0; $i < 5; $i += 2) {
        echo $i, PHP_EOL;
    }
  7. Question 7 of 10 What does count(['a' => 1, 'b' => 2, 'c' => 3]) return?
  8. Question 8 of 10 What does this function return for add(2, 3)?
    PHP
    function add(int $a, int $b = 10): int
    {
        return $a + $b;
    }
  9. Question 9 of 10 You print a name submitted in a form back to the page. Which is safe?
  10. Question 10 of 10 Which statement stops the script with a fatal error if the file is missing, and never loads it twice?