Contents
PHP tutorial
A step-by-step PHP course from scratch: from your first program to advanced topics.
1.Basics
- Introduction to PHPWhat PHP is, where it runs and what a first program looks like.
- VariablesHow to declare variables in PHP, name them and interpolate them into strings.
- Data typesThe main PHP data types — strings, numbers, booleans, arrays and null.
- OperatorsArithmetic, comparison and logical operators in PHP, and operator precedence.
- StringsWorking with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.
- ConstantsConstants in PHP — const and define, naming rules, built-in and magic constants.
- Output and formattingHow to print data in PHP — echo and print, multi-line heredoc text, printf and sprintf templates, number formatting and debug output.
2.Control flow
- ConditionsBranching in PHP — if, else, elseif, the ternary operator, the ?? operator and match.
- LoopsThe while, for and foreach loops in PHP, break and continue, and accumulating a result.
- switchThe switch statement in PHP — choosing among several options, break, shared branches and a comparison with match.
3.Arrays and functions
- ArraysLists and associative arrays in PHP — creating, changing, iterating and useful functions.
- FunctionsHow to declare and call functions in PHP — parameters, return values, types and scope.
- Array functionsProcessing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.
- Sorting arraysSorting in PHP — sort, rsort, asort, ksort, a custom rule with usort and the spaceship operator.
- Anonymous functions and closuresFunctions as values in PHP — anonymous and arrow functions, the use construct, the callable type.
- More about functionsNamed arguments, variable-length argument lists, passing by reference, static variables and recursion in PHP.
- Numbers and mathsPHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.
- Date and timeWorking with dates in PHP — the date function, the DateTimeImmutable class, formats, adding to dates and the difference between them.
- Regular expressionsRegular expressions in PHP — preg_match, preg_replace, preg_split, groups and the basic pattern notation.
4.Object-oriented programming
- Classes and objectsWhat classes and objects are in PHP, how to describe properties and methods and create objects.
- Constructors and encapsulationClass constructors, the public and private modifiers, readonly properties and class constants.
- InheritanceClass inheritance in PHP — extends, protected, overriding methods, parent and polymorphism.
- Interfaces and abstract classesInterfaces and abstract classes in PHP — what they are for, how they differ and how to use them.
- Static properties and methodsThe static keyword in PHP classes — class-level methods and properties, self and static, factory methods.
- TraitsTraits in PHP — reusing code across several classes, properties and abstract methods in traits, resolving conflicts.
- EnumerationsEnumerations (enum) in PHP — cases, values, methods, creating from a string and use in match.
- Magic methodsPHP's magic methods — __toString, __get and __set, __call, __invoke and cloning objects.
- Abstract classes, final and staticAbstract classes and methods, forbidding inheritance with final, the difference between self and static, and the template method.
- Built-in interfacesPHP interfaces that teach an object to behave like an array — Countable, IteratorAggregate, ArrayAccess, JsonSerializable and Stringable.
5.Error handling
6.Files and data
- Reading and writing filesWorking with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.
- The JSON formatJSON in PHP — json_encode and json_decode, non-ASCII text, error handling and storing data in a file.
- Folders and pathsWorking with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.
7.PHP and the web
- Forms and request dataHandling forms in PHP — the $_GET and $_POST arrays, validating data and preventing XSS with htmlspecialchars.
- Sessions and cookiesSessions and cookies in PHP — remembering a visitor between requests, session_start, $_SESSION and setcookie.
- HTTP requests and responsesHow PHP receives a request and builds a response — the request method and address, headers, status codes, redirects and JSON responses.
- File uploadsHow to accept a file from a user in PHP — the form, the $_FILES array, checking the size and type, saving safely.
- SecurityThe basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.
- Calling other sitesHow to call someone else's API from PHP — file_get_contents, the cURL extension, sending JSON, headers, the response code and error handling.
- Sending emailHow to send email from PHP — the mail function, the PHPMailer library and SMTP, HTML emails, attachments and protecting headers.
- Registration and loginHow to build registration and login in PHP — the users table, password hashing, checking on login, the session, protected pages and logout.
8.Databases
- Connecting to a databaseWorking with a database in PHP through PDO — connecting, creating a table, selecting rows and handling errors.
- Prepared statementsPDO prepared statements in PHP — putting data into SQL safely and preventing SQL injection.
- Changing data and transactionsINSERT, UPDATE and DELETE through PDO, the number of affected rows and transactions — how to run several queries as one unit.
- Table relationshipsRelated tables in a database — the foreign key, JOIN, LEFT JOIN, counting with GROUP BY and the problem of extra queries.
- MySQLiWorking with MySQL through the MySQLi extension — connecting, queries, prepared statements, the procedural style and a comparison with PDO.
- PaginationHow to show database records page by page — LIMIT and OFFSET, calculating the offset, the number of pages, links and checking the page number.
- Designing tablesHow to design database tables — column types, the primary key, constraints, foreign keys, indexes and the many-to-many relationship.
9.Project structure
- Namespaces and ComposerHow a PHP project is organised — namespaces, PSR-4 class autoloading and installing libraries with Composer.
- Application structureHow a PHP site is organised — including files, a single entry point, a router, and the split into model, controller and template.
- TestingAutomated tests in PHP — why they matter, installing PHPUnit, the first test, assertions and testing exceptions.
- Command-line scriptsPHP in the terminal — running a script, argv arguments, reading input, exit codes, getopt and running on a schedule.
- Configuration and environmentWhere to keep the settings of a PHP project — a configuration file, environment variables, the .env file, different modes for development and the server.
- Code qualityHow to keep PHP code in order — the PSR-12 standard, automatic formatting, PHPStan static analysis and Composer scripts.
10.Modern PHP
- The type systemTypes in modern PHP — the strict_types mode, nullable and union types, void, never and typed properties.
- GeneratorsGenerators in PHP — the yield keyword, saving memory, keys, infinite sequences and reading large files.
- Modern syntaxPHP 8 features — the ?-> operator, array destructuring, the spread operator, readonly classes, first-class callables and attributes.
- New in PHP 8.4PHP 8.4 features — property hooks, asymmetric visibility, calling a method right after new, and new array functions.