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.

Start learning →

PHP Basics

  1. PHP Introduction — What PHP is, how it powers the web and what you can build with it.
  2. PHP Get Started — Install PHP, run scripts from the terminal and start a local web server.
  3. PHP Syntax — Tags, statements, semicolons and case sensitivity rules.
  4. PHP Comments — Single-line, multi-line and PHPDoc comments.
  5. PHP Variables — Create variables with $, name them, and understand scope.
  6. PHP echo and print — Output text with echo, print, printf and string interpolation.
  7. PHP Data Types — Strings, integers, floats, booleans, arrays, objects and null.
  8. PHP Strings — Single vs double quotes, concatenation, heredoc and common string functions.
  9. PHP Numbers and Math — Integers, floats, rounding, random numbers and number formatting.
  10. PHP Type Casting — Convert between types with casts and conversion functions.
  11. PHP Constants — Define constants with const and define, and use magic constants.
  12. PHP Operators — Arithmetic, comparison, logical, null coalescing and the spaceship operator.
  13. 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

  1. PHP If Else — Make decisions with if, elseif, else and the ternary operator.
  2. PHP Switch and Match — Compare one value against many with switch and the PHP 8 match expression.
  3. PHP Loops — Repeat code with while, do-while, for and foreach.

Functions and Arrays

  1. PHP Functions — Define functions with typed parameters, defaults, named arguments and return types.
  2. PHP Arrays — Indexed, associative and multidimensional arrays.
  3. PHP Array Functions — Sort, search, filter, map and reduce arrays.

PHP for the Web

  1. PHP Superglobals — $_GET, $_POST, $_SERVER and other built-in request variables.
  2. PHP Form Handling — Handle POST forms, validate fields and show errors.
  3. PHP Include and Require — Split code into files with include, require and require_once.
  4. PHP File Handling — Read, write and append files, and work with CSV.
  5. PHP Cookies and Sessions — Remember users between requests with cookies and sessions.
  6. PHP JSON — Encode arrays and objects as JSON and decode JSON into PHP.
  7. PHP Date and Time — Format dates, do date arithmetic and handle time zones.
  8. PHP Regular Expressions — Match, extract and replace text with preg_ functions.
  9. PHP Exceptions — Throw and catch exceptions, use finally and create custom exception types.

Object-Oriented PHP

  1. PHP Classes and Objects — Define classes with properties and methods, and create objects.
  2. PHP Constructors — Initialise objects with __construct and constructor property promotion.
  3. PHP Inheritance — Extend classes, override methods and call parent methods.
  4. PHP Interfaces and Abstract Classes — Define contracts with interfaces and share code with abstract classes.
  5. PHP Traits and Static Members — Reuse methods with traits and use class-level static properties and methods.
  6. PHP Enums — Model a fixed set of values with pure and backed enums (PHP 8.1).
  7. PHP Namespaces — Organise code into namespaces and import classes with use.

Databases and Tools

  1. PHP PDO and Databases — Connect to MySQL, PostgreSQL or SQLite with PDO and run safe prepared statements.
  2. PHP Composer — Install packages, autoload classes and manage dependencies with Composer.

Next Steps

  1. PHP Next Steps — Frameworks, testing and interview practice after the PHP basics.