Free Handbook · Every example compiled & verified

PHP Handbook

Learn PHP the way it is written at work. Seventeen modules from your first script to a job: every example program was run with PHP 8.5 before publishing, with its output shown exactly as it printed, and the real errors and warnings you will hit are explained with the fix. Variables and types, strict comparison, flow control and match, strings and UTF-8, arrays and their functions, functions and closures, classes, enums and readonly objects, interfaces and traits, exceptions, files, JSON and dates, web basics with sessions, PDO and password hashing, debugging, PHP with Composer, Laravel, Symfony, MySQL, Redis, PHPUnit, Docker and GitHub Actions, data structures and algorithms, problem-solving patterns, interview questions, a certification exam, and a resume check at the end.

0 / 148 lessons🔥 0 day streak
ShareXLinkedIn

How this handbook works

Every lesson has a complete PHP program with the exact output it prints — each one compiled and run with the real PHP toolchain before it was published, so what you see is what you will get when you run it yourself — a trace of what the program did line by line where that matters, and the real compiler or runtime error you will hit next, with why it happened and the fix. Lessons tick themselves as you go. Finish the handbook, sit the exam, get the certificate, then take your resume through the ATS checker against a real job posting.

17modules
148lessons
135compiled examples
58real errors explained

Module 00 · what you'll be able to do

  • Explain what PHP is, where it runs, and what happens between a browser request and a PHP script
  • Install PHP 8 on macOS, Windows or Linux and check it with php -v
  • Run a script with php main.php and serve a page with the built-in server php -S localhost:8000
  • Read the structure of a PHP file: the opening tag, statements, echo and print, and comments
  • Use this handbook, its verified examples and an AI assistant without shortcutting your own learning
01

What PHP is and where it runs

PHP is a general-purpose scripting language built for the web. Rasmus Lerdorf released the first version in 1995 as "Personal Home Page Tools"; today the name stands for PHP: Hypertext Preprocessor. You write .php files, and the PHP engine (the Zend Engine) reads, compiles and runs them. There is no separate build step: change the file, run it again, see the result.

PHP mostly runs on a server. A browser never sees your PHP code; it only sees what the script prints — usually HTML or JSON. That is the key difference from JavaScript in the browser.

  1. 1
    1. A browser asks for a page

    It sends an HTTP request for https://shop.example/cart.

  2. 2
    2. The web server hands it to PHP

    Nginx or Apache passes the request to PHP-FPM, a pool of PHP worker processes.

  3. 3
    3. Your script runs

    It reads the request, talks to a database, and echoes HTML or JSON.

  4. 4
    4. The output goes back

    Everything the script printed becomes the HTTP response. Then the script ends and its memory is thrown away — every request starts fresh.

  • WordPress — the content system behind a very large share of all websites, written in PHP. Themes and plugins are PHP.
  • Laravel and Symfony — the two big PHP frameworks for web apps and APIs. Laravel is the most popular; Symfony components sit inside Laravel, Drupal and many others.
  • E-commerce and CMS — WooCommerce, Magento (Adobe Commerce), Drupal, Joomla and MediaWiki (Wikipedia) are PHP.
  • Command-line tools — the same PHP runs scripts, cron jobs and queue workers from the terminal, which is how every example in this handbook runs.
Is PHP "dead"?
No. PHP 8 is a different language from the PHP 5 that earned the bad reputation: it has a JIT compiler, strict typing, union types, enums, readonly properties, match, named arguments, attributes, property hooks and a pipe operator. This handbook teaches that modern PHP, and every example is run with PHP 8.5.
02

Why learn PHP (and the jobs it leads to)

PHP is one of the fastest ways from zero to a working, deployed web app. Cheap hosting runs it out of the box, frameworks like Laravel give you login, database access and queues on day one, and there is a huge amount of existing PHP code that companies need people to maintain and extend.

RoleWhere PHP shows upWhat to learn after this handbook
Backend / full-stack developerLaravel or Symfony APIs, admin panels, SaaS productsLaravel, SQL, a front-end framework
WordPress developerCustom themes, plugins, WooCommerce storesWordPress hooks, the block editor, MySQL
E-commerce developerMagento, WooCommerce, Shopware, payment integrationsComposer, caching, queues
Freelancer / agency developerClient sites, migrations, fixes on existing PHP projectsGit, Docker, deployment on Linux
In real jobs
A lot of PHP work is reading other people's code: an old WordPress plugin, a Laravel app written five years ago, a PHP 7 codebase being upgraded to PHP 8. The fundamentals in Modules 01–10 — types, arrays, functions, classes, exceptions — are exactly what you need to read that code safely. Module 16 covers what a PHP resume must show.
03

Install PHP on macOS, Windows and Linux

You need the PHP command-line interface (CLI). Install it, open a new terminal, and run php -v. If it prints a version starting with 8, you are ready.

bashmacOS (Homebrew)
brew install php
php -v
bashWindows (PowerShell, Scoop)
# install Scoop once from https://scoop.sh, then:
scoop install php
# close and reopen PowerShell, then:
php -v

Without Scoop: download the "VS17 x64 Non Thread Safe" zip from windows.php.net, unzip it to C:\php, and add that folder to Path in System Properties, Environment Variables.

bashLinux (Ubuntu / Debian)
sudo apt update
sudo apt install php-cli php-mbstring
php -v

Distribution packages can lag a version or two behind. Any PHP 8.x runs almost every example here; for the newest release on Ubuntu, add the widely used ondrej/php PPA.

bashExpected output (numbers vary)
PHP 8.5.10 (cli) (built: Aug 25 2026 21:09:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.10, Copyright (c), by Zend Technologies
"php: command not found" or "php is not recognized"
PHP is installed but not on your PATH. Open a fresh terminal first — installers change PATH only for new sessions. On Windows, check that the folder holding php.exe is listed in Path. php --ini shows which php.ini configuration file is loaded, and php -m lists the installed extensions.

For an editor, VS Code with the "PHP Intelephense" extension is free and light; PhpStorm is the IDE most PHP teams pay for. Either one underlines many mistakes before you run the script.

04

Your first PHP program

Create a folder, save the code below as main.php, open a terminal in that folder and run php main.php.

phpmain.php
<?php
echo "Hello, PHP!\n";
echo "2 + 3 = " . (2 + 3) . "\n";
echo "Running PHP " . PHP_MAJOR_VERSION . "\n";
Outputcompiled & run with real PHP
Hello, PHP!
2 + 3 = 5
Running PHP 8
Your turn

Add a line that prints your name. Then delete the \n at the end of the first echo and predict how the output changes before you run it.

bashRunning it
php main.php        # run the script, print to the terminal
php -l main.php     # lint only: check the syntax without running
php -a              # interactive shell, type PHP line by line
VisualizeWhat PHP does when you run itStep 1 / 5
<?php
echo "Hello, PHP!\n";
echo "2 + 3 = " . (2 + 3) . "\n";
echo "Running PHP " . PHP_MAJOR_VERSION . "\n";
Line 1

<?php switches from plain text to PHP mode. Everything before it would be printed as-is; everything after it is code.

Variables now

nothing yet

All 5 steps as a table
StepLineWhat happenedVariables now
11<?php switches from plain text to PHP mode. Everything before it would be printed as-is; everything after it is code.
22echo prints a string. Inside double quotes, \n is a newline character.
33The brackets run first: 2 + 3 is the int 5. The dot . joins strings, so 5 is converted to "5" and glued on.
44PHP_MAJOR_VERSION is a built-in constant holding the running major version.
54The end of the file — the script exits with status 0.
Error you will hit

Parse error: unexpected token — the missing semicolon

php
<?php
echo "Hello"
echo "World";
Parse error: syntax error, unexpected token "echo", expecting "," or ";" in main.php on line 3
Why PHP said that

Every PHP statement ends with a semicolon. PHP reads on past the end of line 2, finds another echo where it expected , or ;, and reports the line where it got confused — which is often the line after the real mistake. Nothing runs: a parse error stops the whole file before the first statement executes.

The fix

Add the semicolon at the end of line 2. Run php -l main.php to check syntax without running.

php
<?php
echo "Hello";
echo "World";
Error you will hit

Call to undefined function — a name from another language

php
<?php
println("Hello");
Fatal error: Uncaught Error: Call to undefined function println() in main.php:2
Stack trace:
#0 {main}
  thrown in main.php on line 2
Why PHP said that

PHP has no println (that is Java or Kotlin), no print() with a newline, no console.log. PHP only discovers a function is missing when the line runs, so this is a runtime Error, not a parse error — any lines before it would already have printed.

The fix

Use echo and add the newline yourself ("\n" or the constant PHP_EOL).

php
<?php
echo "Hello", PHP_EOL;
Where do the error messages go?
The PHP CLI prints errors to the terminal. Depending on your php.ini you may see them once as PHP Fatal error: ... (the log line, on stderr) and once as Fatal error: ... (on stdout), with your full file path. The error cards in this handbook shorten the path to main.php.
05

Serve a web page with php -S

PHP ships with a small web server for development. Save the file below as index.php, run the command in the same folder, and open http://localhost:8000 in your browser. Refresh after every edit — there is nothing to rebuild.

phpindex.php
<!doctype html>
<title>My first PHP page</title>
<h1>Hello from PHP</h1>
<p>2 + 3 = <?= 2 + 3 ?></p>
<p>Your browser sent: <?= htmlspecialchars($_SERVER['HTTP_USER_AGENT'] ?? 'unknown') ?></p>

Text outside <?php ... ?> is sent as-is. <?= expr ?> is short for <?php echo expr; ?>. htmlspecialchars() escapes user input so it cannot inject HTML — Module 10 explains why that matters.

bashTerminal
php -S localhost:8000
# PHP 8.5.10 Development Server (http://localhost:8000) started
# press Ctrl+C to stop
Development only
The built-in server handles one request at a time and has no security hardening. In production PHP runs behind Nginx or Apache with PHP-FPM (Module 12 sets that up with Docker). For learning, php -S is all you need.
06

How a PHP script is structured

A PHP file starts in HTML mode: anything outside PHP tags is printed exactly as written. <?php switches into PHP mode. A file that is pure PHP — every example here — starts with <?php on line 1 and leaves out the closing ?>, so a stray blank line at the end of the file can never leak into the output.

Inside PHP mode you write statements, each ending in ;. They run top to bottom. Variables start with $. Code blocks for if, loops and functions use braces { }; indentation is for humans only.

phpmain.php
<?php
// a variable: $ then a name
$language = "PHP";

// a function other code can call
function square(int $n): int
{
    return $n * $n;
}

echo "Learning $language\n";          // variables work inside double quotes
echo "7 squared is ", square(7), "\n"; // echo takes several arguments
print "print takes exactly one\n";
$result = print "";                    // print returns 1, echo returns nothing
echo "print returned $result", PHP_EOL;
Outputcompiled & run with real PHP
Learning PHP
7 squared is 49
print takes exactly one
print returned 1
Your turn

Add a function cube(int $n): int and print cube(3). Then try print "a", "b"; and read the parse error.

echoprint
KindLanguage construct (not a function)Language construct that behaves like an expression
ArgumentsOne or more, separated by commasExactly one
Return valueNone — cannot be used inside an expressionAlways 1
Use it forAlmost everything — it is what PHP code usesRarely; you will see it in older tutorials

What is case-sensitive?

PHP is half and half. Variables and constants are case-sensitive: $name and $Name are two different variables. Keywords, function names and class names are not: ECHO, Strlen() and STRLEN() all work. Do not rely on that — write keywords and functions in lower case like every PHP codebase does.

phpmain.php
<?php
$name = "Asha";
$Name = "Ravi";            // a different variable
ECHO $name, " and ", $Name, "\n";
echo STRTOUPPER($name), "\n"; // works, but write strtoupper()
Outputcompiled & run with real PHP
Asha and Ravi
ASHA
Naming conventions (PSR-1 and PSR-12)
The PHP community follows the PHP-FIG coding standards: variables and functions in camelCase ($totalPrice, getUser()), classes in PascalCase, constants in UPPER_SNAKE_CASE, four-space indentation, and the opening brace of a function or class on its own line. PHP does not enforce them; tools like PHP-CS-Fixer do.
07

Comments

PHP has three comment styles, plus the docblock. PHP ignores them all; they are for the next person reading the code. Comment why something is done, not what an obvious line does.

phpmain.php
<?php
/**
 * A docblock: IDEs and tools like PHPStan read the tags in it.
 *
 * @param int $seats number of seats booked
 */
function describe(int $seats): string
{
    return "Seats: $seats";
}

// A single-line comment runs to the end of the line.
# A hash also starts a single-line comment (common in older code).
$seats = 42; // it can also follow code

/* A block comment
   can span several lines. */
echo describe($seats), "\n";
// echo "This line is commented out, so it never runs\n";
Outputcompiled & run with real PHP
Seats: 42
Your turn

Remove the // in front of the last echo and run it again — "commenting out" is how you switch a line off while debugging.

#[ is not a comment
Since PHP 8, #[...] is attribute syntax (metadata such as #[Override] on a method). A # followed by a space is still a comment. Prefer // in new code to avoid the confusion.
Quick check

A file page.php contains Hi <?php echo 1 + 1; ?> there. What does php page.php print?

08

How this handbook works

Seventeen modules, each its own page, from variables to a job-ready resume. The rail on the left lists them; the bar at the top and the pill at the bottom-left track your progress. A lesson ticks itself when you reach its end, and the tick survives closing the tab — no account needed.

  • Examples are run and verified. PHP cannot run inside a web page, so every example here is a complete script that was run with the real PHP 8.5 CLI before publishing. The output shown under it is exactly what it printed — copy the script into your editor, save it as main.php, and run php main.php to see the same result.
  • "Your turn" tasks under each example ask you to change one thing. Do them — predicting the output before you run is the fastest way to learn.
  • Traces step through a script line by line with the variables at each step — used where loops, references and recursion get confusing.
  • Error cards show the real PHP message (parse error, warning, TypeError…), why it happened, and the fix. Module 11 collects the fifteen most common.
  • Interview questions are collected in Module 15, tiered junior, mid and senior.
  • The handbook ends with a certification exam (25 questions, 70% to pass), then the ATS resume checker with a real job posting.
The floating board
The pill at the bottom-left opens a board with every key term from the module you are on and a tutor you can ask about the code on the page. It is a language model — check what it says against a real php run.
ModulesYou learn
01–04Variables, types, strict_types, operators, if/match, loops, strings, arrays
05–08Functions and closures, classes and objects, inheritance, interfaces, traits, enums, exceptions
09–10Files, JSON and dates; PHP for the web — forms, sessions, cookies and databases with PDO
11–14Errors and debugging, PHP with real tools (Composer, Laravel, Docker, MySQL…), data structures and algorithms, problem solving
15–16Sixty interview questions, resume, portfolio, the certificate
09

Using AI to learn PHP (without cheating yourself)

An AI assistant can write a working Laravel controller in seconds. That is exactly why you should type the code yourself while learning. The goal is not the code; it is the model in your head — why "abc" == 0 used to be true, why strpos() needs !== false, why a foreach by reference corrupts the next loop. That model is what an interviewer tests and what lets you fix a production error log at 2 a.m.

Do this

  • Paste a real PHP error and ask what it means, then fix it yourself
  • Ask "why does this print 1?" after you have made your own guess
  • Write the function first, then ask for a code review
  • Ask for three edge-case inputs (empty string, null, a UTF-8 name) your code might get wrong

Not this

  • Ask for the answer to a "Your turn" task and paste it in
  • Accept code you cannot explain line by line
  • Silence a warning with @ because the AI suggested it
  • Use it during an interview or the certification exam

Ready? Module 01 starts with variables, PHP's types, strict_types, operators and null handling.

Every lesson in the handbook

17 modules · 148 lessons · each one ticks itself when you reach the end.

00Getting Started0 / 9
01Fundamentals0 / 8
02Flow Control0 / 8
03Strings0 / 8
04Arrays0 / 10
05Functions0 / 8
06Classes & Objects0 / 8
07Inheritance, Interfaces & Traits0 / 8
08Exceptions0 / 8
09Files, JSON & Dates0 / 8
10PHP for the Web0 / 8
11Errors & Debugging0 / 7
12PHP + Tools0 / 10
13Data Structures & Algorithms0 / 12
14Problem Solving0 / 12
15Interview Questions0 / 10
16Job Ready0 / 6

Frequently asked questions

Is this PHP handbook free?
Yes — every module, every example and the certification exam are free, with no signup and no paywall. The exam asks you to sign in with Google so the certificate can carry your name.
Do I need to know another programming language first?
No. Module 01 starts from variables and types and assumes nothing. If you already know another language you will move faster, and the handbook calls out where PHP differs, such as loose versus strict comparison, arrays that are ordered hash maps, arrays passed by value while objects are passed as handles, and the request-per-process model.
Do I need to install anything?
Yes — PHP itself, and Module 00 shows how to install it on macOS, Windows and Linux and run your first script. The examples on this page do not run in the browser; instead, each example program was run with PHP 8.5 before publishing, and the output shown is exactly what it printed, so you can paste it into main.php, run php main.php and get the same result. The labs in Module 12 need Composer packages and services such as Laravel, MySQL and Redis, so they are shown as clearly marked snippets.
Which version of PHP does this handbook use?
PHP 8, with every example verified on PHP 8.5, and it teaches the modern language: strict types, union types, match, named arguments, constructor property promotion, enums, readonly properties and classes, first-class callables and property hooks. Where a feature needs a recent version, the lesson says which one, so you know what runs on older servers.
Is there a certificate?
Yes. Pass the 25-question exam (35 minutes) with 70% or more and generate a SolutionGigs PHP certificate with your name, score, date and a certificate ID — download it, print it or add it to LinkedIn.
Is PHP still worth learning?
Yes. PHP runs WordPress, Laravel, Symfony, Drupal and Magento, so a very large part of the web is PHP, and companies keep hiring people to build and maintain it. Modern PHP 8 has strict types, enums, match and a JIT, so it is a far better language than its old reputation suggests.
Is PHP hard to learn for a beginner?
PHP is one of the easier languages to start with: no compile step, forgiving syntax, and instant results in the browser. The hard parts are its older quirks — loose comparison, type juggling, functions that return false on failure — which this handbook teaches head-on, along with declare(strict_types=1) to avoid them.
Should I learn PHP or JavaScript first?
If you want to build websites and freelance, or work with WordPress or Laravel, start with PHP — it gets a database-backed site online fastest. If you want front-end or full-stack JavaScript work, start with JavaScript. Most web developers end up knowing both.
Which PHP version should I install?
Install PHP 8.3 or newer; every example in this handbook was run with PHP 8.5. Avoid PHP 7 and older: they no longer receive security fixes and lack the PHP 8 features taught here.

Finish the PHP handbook, then get hired

Sit the exam for your certificate, run your resume through the ATS checker, and see the jobs that ask for exactly this.

Check my resume
Found this course useful? Share it.
ShareXLinkedIn

Comments

0

Join the conversation. Sign in to leave a comment — we'd love to hear your thoughts.