When I advise people about upgrading their PHP version, I say things like "just run your test suite with the new version" "just grab the new version and try your site with the built-in webserver". A couple of people recently have asked for more detail on how to actually achieve these things so here's a quick primer on getting new PHP without touching anything to do with your existing PHP installation.

Install Your New PHP Version

(These instructions are for nix systems; I have literally no idea how it works on Windows)

You will want to download the source code of your desired version of PHP, then compile it. The key thing here is that we'll put this version of PHP in a different location than your operating system would like to put PHP by default - and so we'll avoid overwriting anything.

Go and choose your version from http://php.net/downloads.php. Download it into its own directory somewhere and extract the files.

Now we'll configure the source, but here is the key ingredient: we set a prefix so that it'll be installed into a separate directory and not interfere with anything. Make a command like this with your path in:

./configure --prefix=/path/to/toy/php
Now we're all set and we can compile the code and install it to the related location:

make install
The make step builds the binaries and the make install step moves them to the correct location - so you may need tosudo make install if your user doesn't have write permission to the location you set in the prefix earlier on.

You're basically done Check everything is working by doing:

/path/to/toy/php/bin/php -v
This should show you what version of PHP you just built.

Checking Your Application With Your New PHP Version

Whatever you have in place for your usual test/build process, you can go through and have it use your/path/to/toy/php/bin/php everywhere it would usually run a php command. This can mean fiddling with environment variables or hardcoding paths in places while you try this out - so if you modify your tools make sure you're making those changes on a branch in your source control tool.

To test out your code with the webserver, you just need to start it using the new PHP version. For me, that command looks like this:

/path/to/toy/php/bin/php -S localhost:8080
I am seeing more PHP projects being upgraded now, where once they would have been shipped onto a set version, deployed to the server, and left there until they needed replacing. Now we build applications, not just websites, and those need to grow and live along with the companies they exist to serve. The much smoother upgrade process in PHP as well as tools like the webserver make upgrading platforms a perfectly advisable thing to do and I hope the outline above helps someone to make that leap!

View more threads in the same category: