The PHP documentation now allows you to execute code examples directly within the page

phpwasmserver-set-up
timeToRead2 min read

Author
Sergey Kualkov is software expert with 10+ years of experience. He has worked in Healthcare, Automotive, Radio, Education, Fintech, Retail E-commerce, Business Sector, Media and Entertainment. Sergey led the development of 20+ products, and successfully re-engineered the business process for established products, which allowed those products to grow the user base and revenue up to 5 times
The PHP documentation now allows you to execute code examples directly within the page

The official PHP documentation now features a "Run code" button above each code example, allowing you to execute the code and instantly see the result. Additionally, you can modify the code, enabling you to experiment with it or adapt it to your specific needs.

At present, this functionality is in a testing phase and serves more as a demonstration. While many bugs will likely be identified and resolved over time, the convenience of this feature is undeniable. Future plans include adding the ability to execute code for different versions of PHP.

This feature leverages WebAssembly (WASM) technology, meaning that, unlike most online sandboxes that send code to a server for execution, the execution occurs directly in your browser. When you run an example, a 4MB php-web.wasm file is loaded, which contains the PHP interpreter. One of the clear benefits of this approach is that it eliminates server security and performance issues, since all code is executed locally in the user's browser.

This method has already been used in the popular 3v4l.org sandbox for live preview mode for some time, so it was only a matter of time before it was incorporated into the PHP documentation—thanks in part to a community-driven pull request.

However, there are some limitations. Examples that rely on server-side infrastructure won't work in this setup. For these cases, you'll still need to use a local server or third-party sandboxes like phpize.online, which offers unique integration with popular databases.

Moreover, the functionality is currently limited to core PHP features, and extensions are not supported. For instance, examples using the mbstring extension won't work (though these examples are rare). That said, most standard functions, such as the recently added array_find, work without issue.

It’s also important to note that many examples aren’t yet fully adapted for live execution and may return errors due to missing context. A notable example is the "enumeration examples" page, where the first two examples don’t produce any output, and the third throws an error because it depends on code from the previous example.

Fortunately, these issues can be easily addressed. A new improvement now allows you to submit fixes directly: at the bottom of each documentation page, you'll find a "Submit a Pull Request" link that leads to the page's source in the official documentation repository on GitHub. This makes suggesting improvements as simple as fixing a bug in your own code.

Author
Sergey Kualkov is software expert with 10+ years of experience. He has worked in Healthcare, Automotive, Radio, Education, Fintech, Retail E-commerce, Business Sector, Media and Entertainment. Sergey led the development of 20+ products, and successfully re-engineered the business process for established products, which allowed those products to grow the user base and revenue up to 5 times