PHPPHP

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.

WebsitesCMS (WordPress)Web backends

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 --version

You 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 shell

For 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 root
TipXAMPP/MAMP provide Apache, PHP and MySQL in one package – handy if you work with a database. If you like things reproducible, use Docker: docker run --rm -p 8000:80 -v "$PWD":/var/www/html php:8.3-apache starts Apache with PHP and your folder.
WarningThe built-in server (php -S) is meant for development only. In production, PHP scripts run behind nginx or Apache with PHP-FPM.

PHP learning path

Beginner
Intermediate
Advanced

At a glance

First released1995
CreatorRasmus Lerdorf
ParadigmMulti-paradigm (OOP, procedural)
TypingDynamic (with optional types)
ExecutionInterpreted (server-side)
File extension.php

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

PHP compared

Hello World: PHP comparedVariables: PHP comparedConditionals: PHP comparedLoops: PHP comparedFunctions: PHP comparedLists and Arrays: PHP comparedDictionaries and Maps: PHP comparedClasses and Objects: PHP comparedError Handling: PHP comparedReading Files: PHP compared