PHP 7 to 8 upgrade breakage and other PHP traps
phpphp8migrationpreg-replacepassword-hashlegacy
What broke when the tldr API moved from PHP 7.3 (CentOS 7) to PHP 8.3 (Alma 9), plus a few general PHP traps found later. Most PHP 8 breakage is in code that leaned on silent nulls and loose typing; it shows up as TypeErrors and deprecation warnings, not as obvious failures.
PHP 8 changes that bit
- Null passed to string functions.
strlen(null)is deprecated in 8.1 and noisy; fix withstrlen($x ?? ''). - Array access on a null return.
error_get_last()['message']fatals when there was no error. Use(error_get_last() ?? [])['message'] ?? 'Unknown error'. - Dynamic properties are deprecated (8.2). Declare every property the class assigns
(
private $em = '';). - Constructors cannot return values meaningfully; a
return falsein a constructor was removed. - Short open tags
<? echostop working whenshort_open_tagis off; use<?php echoor<?=. - Strict typing in libraries: PSR-7
write(null)throws a TypeError (see Slim 4 PHP APIs (and migrating from Limonade)). - Removed functions that only old frameworks used:
get_magic_quotes_gpc(),get_magic_quotes_runtime(),$str{0}curly-brace offsets. These lived in Limonade's vendor file, which is a reason to replace an unmaintained framework rather than patch it. - Latent bugs surface: a
fetch()with no precedingexecute()had "worked" by accident.
preg_replace eats bcrypt hashes
Writing a password hash into a config file with preg_replace corrupted it: the replacement
string $2y$10$... was read as backreferences ($2, $10), so the stored hash lost its prefix
and the login was rejected. Insert hashes (or any string containing $) with plain string
splicing, str_replace, or preg_replace_callback, and password_verify() the stored value
before trusting it.
Running PHP over SSH
Multi-line PHP through ssh host "php -r '...'" is quoting hell (shell, then PHP, then
$ variables). Upload a small script, run it, delete it. That was also the pattern on the old
CentOS 7 server, which had no Python 3: write a PHP script via SSH, run it with /usr/bin/php,
remove it.
php-fpm specifics
php_value in .htaccess is a 500 under php-fpm; set limits in /etc/php.ini. See
Setting up an Alma Linux 9 LAMP server.
Small PHP habits that paid off
- Money as integer cents; never floats.
switchfall-through can be deliberate (anuploadcase falling intoread); comment it.- Keep secrets in a server-only
config.php(mode 640, owner deployer, group apache) that the deploy excludes, with a committedconfig.example.php.