Learn PHP – tutorial, cheatsheet & exercises
PHP is the language that powers a huge part of the web: WordPress, Shopware, Nextcloud, Wikipedia (MediaWiki) and frameworks like Laravel and Symfony all run on PHP. A PHP script runs on the server, generates HTML (or JSON) and sends it to the browser – the user never sees the PHP code.
Modern PHP 8.x has little in common with the PHP of ten years ago: real types (int|string, ?User), match, enums, readonly properties, named arguments, attributes and fibers. On top of that come Composer as the package manager and PDO for secure database access. This page shows you PHP the way it is written today – including the security basics (XSS, SQL injection, password_hash) that are not optional on the web.
Hello World in PHP
<?php
echo "Hello World!\n";echo prints text. Run it with php datei.php or via the web server.
is a line break.
How to get started with PHP
Install PHP 8.2 or newer: on a Mac with brew install php, on Windows with winget install PHP.PHP (or a bundle like XAMPP), on Linux with sudo apt install php-cli. Check the version:
php --versionYou run PHP files directly in the terminal. This works without a web server and is ideal for learning:
php hallo.php
php -r "echo 2 + 3;"
php -a # interactive shellFor web pages you need a server. PHP ships with a built-in development server that serves the current folder. Afterwards, open http://localhost:8000 in the browser:
php -S localhost:8000
php -S localhost:8000 -t public # folder public/ as web rootdocker run --rm -p 8000:80 -v "$PWD":/var/www/html php:8.3-apache starts Apache with PHP and your folder.php -S) is meant for development only. In production, PHP scripts run behind nginx or Apache with PHP-FPM.PHP learning path
Hello World & PHP Tags
Your first PHP script, echo and how PHP works together with HTML.
Variables, Types & Strings
Variables with $, the basic types and the difference between '...' and "...".
Conditions, ?? and match
if/elseif, ternary, null coalescing and the modern match expression.
Loops: for, while, foreach
Repetition over numbers and arrays.
Functions with Types & Named Arguments
Parameters, return types, default values, variadics and arrow functions.
Arrays: Lists and Maps in One
The universal PHP array, its functions and destructuring.
Strings, Files & JSON
Processing text (including umlauts), reading/writing files and handling JSON.
Forms, GET & POST
Receiving input, validating it, outputting it safely and finishing with Post/Redirect/Get.
Classes, Interfaces & Traits
Object-oriented programming with constructor promotion, inheritance, interfaces and traits.
Enums & readonly
Type-safe choice values and immutable objects – two highlights of PHP 8.1+.
Exceptions & Error Handling
try/catch/finally, custom exceptions, Error vs. Exception and strict_types.
Sessions, Cookies & Login
Storing state between requests and building a secure login.
Composer, Namespaces & Autoloading
Installing packages and organizing your own code cleanly into namespaces.
Database with PDO
Prepared statements, transactions and clean queries with PDO.
Security: XSS, SQL Injection, Passwords
The most important web attacks and how to prevent them in PHP.
Modern PHP: Generators, Attributes, Fibers & Tooling
Memory-friendly data streams, attributes, first-class callables, fibers and quality tools.
At a glance
More about PHP
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: