Run PHP Through A Browser With PHPSandbox

23rd January 2022 - 5 minutes read time

I use sites like CodePen and JSFiddle all the time to write JavaScript and CSS, but I've not seen a similar site for writing PHP applications. I have used 3v4l.org to test small scripts using different versions of PHP, but it doesn't allow dependencies to be added and doesn't have a nice editing interface.

Recently, I found a site called PHPSandbox that allows users to create PHP applications that can be run in the same way as applications on CodePen. This includes using composer to inject dependencies into the application and a nice little editor interface to see the output and logs from the application.

PHP is very easy to learn, it's a versatile and forgiving language, but it's not exactly easy to get up and running for beginners. You could argue that running some apt-get scripts or docker containers is easy, but that is technically a barrier to entry. Languages like JavaScript have a lower barrier as all you need is a web browser and a text editor and there are multiple sites out there that allow you to write and run JavaScript in the browser.

This is where PHPSanbox come in. It allows you to create PHP projects that you can then execute to see the output using PHP 7.4 or 8.0 (at time of writing).

A screenshot of the PHP Sandbox homepage.

You can define the normal things you need for a PHP application like environment variables and composer packages. PHPSandbox also gives you the ability to create applications from skeleton examples using frameworks like Symfony and Slim. Each application you create is called a notebook and can be shared or downloaded easily. Once set up you will have a fully functioning PHP environment that you can develop simple applications with included composer dependencies. If your application outgrows the site then you can always download it and work on it from there.

It is also possible to link PHPSandbox to your GitHub account to allow code to be pulled directly from GitHub into notebooks. I wasn't able to get this working as the permissions created would allow the site any access to any repo I have access to, which includes a few private repositories belonging to companies. I think this is how GitHub permissions work though, and not a problem with PHPSandbox. For a beginner this shouldn't be a problem, I just couldn't accept the risk here.

What PHPSandbox doesn't come with is PHP extensions like the GD2 library, which means you are unable to generate image files. It does have access to Sqlite database so it's possible to plug your applications into a data source.

Each notebook supports a few different mechanisms to allow them to be embedded. The example below if using the iframe embed option (although I had to tweak some of the variables to get it to work correctly).

By running the above code you can see the evolution engine I have been working on (and am currently writing some articles about). All I did was to ensure the package was included as a dependency and then start writing code. If you click the "Run" button on the right you can see the result in the output window.

This can solve an issue on some sites where PHP examples are present, but there is no way to run them. You can't run every example as the sandbox you are given isn't a complete environment, but it will run standard PHP code without any issues.

I've been very impressed with what I have seen here and I would suggest giving PHPSandbox a go.

Add new comment

The content of this field is kept private and will not be shown publicly.