Free tutorial · 38 lessons
PHP Tutorial
Learn modern PHP 8 step by step: syntax, variables, strings, arrays, functions, forms, sessions, files, JSON, classes, interfaces, traits, enums, PDO databases and Composer — with examples you can run in your browser.
PHP Basics
- PHP Introduction — What PHP is, how it powers the web and what you can build with it.
- PHP Get Started — Install PHP, run scripts from the terminal and start a local web server.
- PHP Syntax — Tags, statements, semicolons and case sensitivity rules.
- PHP Comments — Single-line, multi-line and PHPDoc comments.
- PHP Variables — Create variables with $, name them, and understand scope.
- PHP echo and print — Output text with echo, print, printf and string interpolation.
- PHP Data Types — Strings, integers, floats, booleans, arrays, objects and null.
- PHP Strings — Single vs double quotes, concatenation, heredoc and common string functions.
- PHP Numbers and Math — Integers, floats, rounding, random numbers and number formatting.
- PHP Type Casting — Convert between types with casts and conversion functions.
- PHP Constants — Define constants with const and define, and use magic constants.
- PHP Operators — Arithmetic, comparison, logical, null coalescing and the spaceship operator.
- PHP History & Versions — How PHP grew from Rasmus Lerdorf's personal home page tools into the language behind a large share of the web, every major release on one timeline, the big leaps in PHP 7 and 8, and which version to run today.
Control Flow
- PHP If Else — Make decisions with if, elseif, else and the ternary operator.
- PHP Switch and Match — Compare one value against many with switch and the PHP 8 match expression.
- PHP Loops — Repeat code with while, do-while, for and foreach.
Functions and Arrays
- PHP Functions — Define functions with typed parameters, defaults, named arguments and return types.
- PHP Arrays — Indexed, associative and multidimensional arrays.
- PHP Array Functions — Sort, search, filter, map and reduce arrays.
PHP for the Web
- PHP Superglobals — $_GET, $_POST, $_SERVER and other built-in request variables.
- PHP Form Handling — Handle POST forms, validate fields and show errors.
- PHP Include and Require — Split code into files with include, require and require_once.
- PHP File Handling — Read, write and append files, and work with CSV.
- PHP Cookies and Sessions — Remember users between requests with cookies and sessions.
- PHP JSON — Encode arrays and objects as JSON and decode JSON into PHP.
- PHP Date and Time — Format dates, do date arithmetic and handle time zones.
- PHP Regular Expressions — Match, extract and replace text with preg_ functions.
- PHP Exceptions — Throw and catch exceptions, use finally and create custom exception types.
Object-Oriented PHP
- PHP Classes and Objects — Define classes with properties and methods, and create objects.
- PHP Constructors — Initialise objects with __construct and constructor property promotion.
- PHP Inheritance — Extend classes, override methods and call parent methods.
- PHP Interfaces and Abstract Classes — Define contracts with interfaces and share code with abstract classes.
- PHP Traits and Static Members — Reuse methods with traits and use class-level static properties and methods.
- PHP Enums — Model a fixed set of values with pure and backed enums (PHP 8.1).
- PHP Namespaces — Organise code into namespaces and import classes with use.
Databases and Tools
- PHP PDO and Databases — Connect to MySQL, PostgreSQL or SQLite with PDO and run safe prepared statements.
- PHP Composer — Install packages, autoload classes and manage dependencies with Composer.
Next Steps
- PHP Next Steps — Frameworks, testing and interview practice after the PHP basics.