When I try to figure how something works in my programming language I often use the service replit. It offers a simple bare bones php environment which is ready to go to test out some stuff, is portable and free to use.
One thing that is slightly annoying is that they only support PHP 7.4 out of the box, but it is very easy to upgrade the php version used to PHP 8. Let’s start with an example:
<?php
$str = "Hello, world!\n";
if (str_contains($str, 'llo')){
echo 'YUP';
}
This code will not run as is on replit, because the function str_contains doesn’t exist before PHP 8.
So let’s change that. Click the three dots in the side bar and reveal hidden files:
Next, open the replit.nix
file and change the used php version, like so:
{ pkgs }: {
deps = [
pkgs.php //from pkgs.php74
];
}
Without needing to do anything else we have instructed nix - the package and config manager underlying much of replit.com’s functionality - to use the latest php package which happens to be php 8.
If we run our little test program now, it’ll work:
NB: The version of nix on replit is not up to date, so trying to use php82
to get the latest and greatest PHP Version 8.2 won’t work. But php 8.0 is still better than php 7.4