In this chapter
We'll learn what PHP extensions are, the difference between built-in and add-on extensions, how to install and switch one on in each operating system, how to check what's loaded, which extensions real projects need — and OPcache, the built-in extension that makes PHP much faster by remembering compiled code.
The Problem in Real Life
Mia writes a small script (a script is a PHP file that PHP runs from top to bottom, like check-name.php) that checks a customer's name is short enough for an SMS reminder. It uses mb_strlen() to count the letters — names like "Zoë" need it. On her laptop it works perfectly.
Marcus has just set up a fresh test server for BookBay, so she copies the script there and runs it. The answer: Fatal error: Uncaught Error: Call to undefined function mb_strlen().
"Same PHP version, same code," she says. "How can a function exist on my laptop and not on the server?" Marcus types php -m on both machines and puts the two lists side by side. One line is missing on the server: mbstring.
Same PHP, same code — but a function exists on my laptop and not on the server?
Mia
The Same PHP, Different Features
PHP is built from parts
Many PHP functions come from extensions — add-on parts that can be present on one machine and missing on another.
Extensions must be switched on
Installing an extension isn't always enough; PHP has to load it, and PHP-FPM needs a restart.
Speed comes from an extension too
Without OPcache, PHP re-reads and re-compiles every file on every request.
PHP Extensions and OPcache
What an extension is: an extension is an add-on part of PHP that gives it new functions and classes. PHP itself is small at its core; most of what you use every day comes from extensions — working with text in any language, talking to databases, sending web requests, handling images. It's like the apps on a phone: the phone works without them, but each one adds abilities.
Extensions are written in the C language and installed into PHP itself — not into your project. That's why a function can exist on one computer and not on another: the PHP code is the same, but the extensions installed next to PHP are different.
There are two kinds:
- Built-in extensions: compiled into PHP and always on. You can't forget them. Examples:
Core,date,pcre(regular expressions),jsonandrandom. - Shared (add-on) extensions: separate files that PHP loads when it starts —
.sofiles on Linux and macOS,.dllfiles on Windows. They must be installed and switched on in php.ini (or a file inconf.d).mbstring,intl,curlandpdo_mysqlare usually this kind.
Installing and switching on an extension: it's different on each system, as the code box below shows. On Ubuntu (and WSL), each extension is a package: install it with apt and it's switched on for you, with a small file added to conf.d. On Windows (without WSL), the extensions already sit in the ext folder next to php.exe — you switch one on by removing the ; in front of its extension= line in php.ini. Homebrew's PHP on macOS already includes the common ones. Extensions that aren't part of PHP itself come from PECL, PHP's extension library, installed with pecl install. Whatever the system: after switching an extension on, restart PHP-FPM, or the website won't see it.
# Ubuntu / WSL — install the package (it's switched on automatically)sudo apt install php8.5-mbstring php8.5-intlsudo systemctl restart php8.5-fpm # so the website sees it too# macOS (Homebrew) — common extensions are already includedphp -m | grep mbstring# Extensions from PECL (e.g. Redis, used later in the course)pecl install redis
extension_dir = "ext"; Off — the line starts with ;;extension=intl; Onextension=mbstring
Checking what's loaded: php -m lists every extension the command line loaded — that's what Marcus compared between the two machines. Inside PHP code, extension_loaded('mbstring') returns true or false. For the website, check phpinfo() too: remember from chapter 5 that the command line and PHP-FPM can have different settings files, so an extension can be on in one and off in the other.
<?phpvar_dump(extension_loaded('mbstring')); // bool(true) when it's loadedif (!extension_loaded('intl')) {echo "The intl extension is missing — ask for php8.5-intl to be installed.\n";}
On the command line: php -m | grep intl (Linux, macOS, WSL) or php -m | findstr intl (Windows).
The extensions most projects need: you don't need to know them all. The table below lists the ones you'll meet in this course, and which Act uses each one. When a tutorial or a package says it "requires ext-intl", it means the intl extension must be installed.
| Extension | What it adds | Used in |
|---|---|---|
| mbstring | Correct work with text in any language ("Zoë", "José") | Act 07 Strings |
| intl | Formatting dates, numbers and money for different countries | Act 07 Strings |
| curl | Sending requests to other websites and APIs (SMS, payments) | Act 16 HTTP, Act 24 |
| pdo_mysql | Talking to a MySQL database | Act 18 Databases |
| openssl / sodium | Encryption and secure connections | Act 20 Security |
| xml / dom | Reading and writing XML | Act 19 Data interchange |
| zip | Creating and opening .zip files | Act 15 Files |
| opcache | Keeps compiled code in memory — speed | This chapter, Act 25 |
| xdebug (PECL) | A debugger for developers — never on production | Act 14, Act 21 |
Extensions vs. packages: later (Act 13) you'll also install Composer packages — libraries written in PHP that live inside your project folder. The difference: an extension is part of PHP on the machine (written in C, installed once per computer), while a package is part of your project (written in PHP, installed per project). Both are "extra code", but they live in completely different places.
OPcache — the extension that makes PHP fast: remember from chapter 2 that PHP first turns your code into small instructions called opcodes, then runs them. Without help, PHP does that compile step for every file on every request — even though the file hasn't changed. OPcache (opcode cache) keeps the compiled opcodes in memory, so the next request skips straight to running them. It's like a barber who memorises a regular customer's usual cut instead of asking every time. For a typical website this makes PHP much faster, for free. Since PHP 8.5, OPcache is always built into PHP — it can't be missing any more, only switched off.
Without OPcache vs. with OPcache
Without OPcache
every request: read file → compile → run
With OPcache
first request: read → compile → store
Compile again
same work, every single time
Next requests
take the stored opcodes → run
OPcache settings: a few php.ini settings control it, shown in the code box below. The important one to understand is opcache.validate_timestamps. With 1 (development), OPcache checks every couple of seconds whether a file changed, so your edits show up. With 0 (production, for extra speed), it never checks — so after each deploy (putting new code on the server) you must restart PHP-FPM, or the server keeps running the old code. Forgetting this is a classic "I deployed but nothing changed" puzzle.
; Development — see your edits immediatelyopcache.enable = 1opcache.validate_timestamps = 1opcache.revalidate_freq = 2 ; check files every 2 seconds; Production — fastest; restart PHP-FPM after every deployopcache.enable = 1opcache.validate_timestamps = 0opcache.memory_consumption = 128 ; MB of memory for compiled codeopcache.max_accelerated_files = 10000
Checking that OPcache works: php -v mentions "with Zend OPcache" when it's loaded. In code, opcache_get_status() returns details such as how many files are cached — but note that OPcache is usually switched off for the command line (opcache.enable_cli = 0), so check it from a web page. OPcache also contains PHP's JIT compiler (chapter 3); it's off by default and helps mostly with heavy calculations, not typical websites — Act 23 looks at it properly.
<?php$status = opcache_get_status(false);if ($status === false) {echo "OPcache is off here.";} else {echo "Cached files: " . $status['opcache_statistics']['num_cached_scripts'];}
Key Takeaway
Extensions are add-on parts of PHP that provide many of its functions. They're installed per computer, not per project, so check php -m (and phpinfo() for the web) when a function is "undefined". OPcache is the extension that keeps compiled code in memory so PHP doesn't recompile every file on every request.
Why This Matters
"Works on my machine, fails on the server" is one of the most common problems in PHP work, and a missing extension is one of its most common causes. Comparing php -m between machines — and writing the needed extensions down in the project's README — turns that problem into a one-minute fix. In Act 13, Composer will even check them for you.
OPcache matters on Launch Day and after it: it's free speed that every production PHP server should have switched on, and its "restart after deploy" rule is something Mia will need every time she ships a change to BookBay's server.
Marcus installs php8.5-mbstring on the test server, restarts PHP-FPM, and the script runs. "Right," he says. "Your laptop has PHP, the right settings and the right extensions. One thing left before Tom's page can load in a browser: a web server." That's the last chapter of this Act.
