Learn PHP
Based on PHP 8.3PHP classes, encapsulation, traits, enums and security, run in a browser.
$this and clone.Example: coffee orders2EncapsulationHide a bank balance behind private and let only deposit and withdraw change it. Starts with the bug you get without hiding.Example: bank account3InheritanceExtend a basic parcel class into express and fragile parcels. Covers extends, parent:: and final.Example: parcel fees4PolymorphismHandle card, bank transfer and points with one pay() call. Covers interfaces and parameter types.Example: payment methods5AbstractionThe abstract parent owns the steps of a parking fee; children only supply their rates. Covers abstract and choosing between interfaces and traits.Example: parking fees6Composition, static, traits, enumsA post holds its author (composition), gets a slug method from a trait and keeps its status in an enum. Also covers static and self versus static.Example: posts and authors7Access modifiersUse shop coupons to compare how far public, protected and private reach, with a table and a runnable example. Also covers readonly and what PHP 8.4 adds.Example: shop coupons+Project structurePick a small website, JSON API, command-line tool or library and get a tree with a public front controller, src, tests and config, plus composer.json and starter files as a zip.zip download!Security basicsInput filtering, output escaping, PDO prepared statements, password_hash, CSRF tokens, uploads and session cookies, each as wrong code next to fixed code.7 wrong → fixed pairs>PHP toolsRun PHP, decode serialize() strings, build date() formats and test preg patterns, all inside your browser.four PHP toolsSuggested order
Start with classes and objects and go in order. After encapsulation you can build one solid object; inheritance, polymorphism and abstraction show how classes work together.
Composition and static covers traits, enums and match; access modifiers covers the PHP rules for private. PHP is a web language, so do read security basics, then finish by building a project folder in project structure.
Where the output comes from
Every example was run with PHP on our server (CLI) and with the in-browser PHP 8.3 behind the Run button (php-wasm), and the two outputs were compared by script. Step-through values were read from the running program at that line.
When you press Run, the code runs inside your browser. Browser PHP is a 32-bit build, so integers top out at 2147483647, and it has no mbstring. On your own computer, run it like this.
php main.php # check the versionphp -vPHP version
Text and examples follow PHP 8.3. Features added in 8.4 or 8.5 are marked as new. In 8.5, compile-time fatal errors gain two Stack trace: lines, and some functions warn when their return value is ignored.
Where PHP came from
PHP grew out of a few small C programs Rasmus Lerdorf wrote in 1994 to look after his personal home page. He released the source as "Personal Home Page Tools" in June 1995. In 1997 Andi Gutmans and Zeev Suraski rewrote it from scratch, and PHP 3 (June 1998) is where the language as we know it starts. PHP 4 (2000) moved to the Zend Engine, PHP 7 (2015) brought a big speed jump and PHP 8 (2020) added a JIT compiler. Open-source contributors keep it going today, together with The PHP Foundation set up in 2021.
Most PHP builds HTML on a web server: WordPress, Laravel and MediaWiki are all PHP. Almost every web host has it installed and a file runs as soon as you upload it, which suits company sites, online shops and admin screens. Long-running real-time servers, desktop apps and heavy number crunching usually go to other languages.
On this site the examples run inside your browser, and the security basics page puts wrong code next to fixed code. The PHP tools page covers unserialize, date() formats and regex tests.
Tools that pair well PHP security basics PHP tools Compare languages
| Made by | Rasmus Lerdorf |
|---|---|
| First release | PHP Tools in June 1995 · PHP 3 in 1998 |
| Run today by | Open-source contributors and The PHP Foundation (since 2021) |
| Used mostly for | Websites, online shops, WordPress, admin screens |
Questions people ask
I am new to PHP. Where should I start?
Start with classes and objects and go in order. The pages assume you know variables, if, foreach and functions. If you write web code, read security basics too.
Where does my code run when I press Run?
In PHP 8.3 inside your browser. The first press downloads about 3 MB, and later runs start at once.
Which PHP version do the pages follow?
Text and examples follow PHP 8.3. Features new in 8.4 or 8.5 are marked, and every example was also run on the latest PHP on our server to confirm the same result.