My site was running fine and dandy for a few years. I made the necessary adjustments to make it compatible with PHP 7.2 and upgraded the server. A few days later I started receiving random 502 errors.
The problem apparently was a simple else if statement within a WordPress filter. Here is the important parts of the code:
function php72_killer() {
for($hour = 8; $hour < 17; $hour++) :
$display_hour = $hour;
$ampm = 'am';
if ( $hour == 0 ) {
$display_hour = 12;
}
else if ( $hour > 12 ) {
$display_hour = $hour - 12;
$ampm = 'pm';
}
else if ($hour==12) {
$ampm = 'pm';
}
endfor;
}
add_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );
That last else if turned out to be the problem? Why? I don’t fully understand, but I found PHP 7.2 introduced optimized else ifs: https://derickrethans.nl/php7.2-switch.html
It’s important to note that this for loop with if-statements outside of the WordPress callback doesn’t cause random 502 errors.
So what fixed my 502 errors? not having a 2nd else if:
function php72_killer() {
for($hour = 8; $hour < 17; $hour++) :
$display_hour = $hour;
$ampm = 'am';
if ( $hour == 0 ) {
$display_hour = 12;
}
else if ( $hour > 12 ) {
$display_hour = $hour - 12;
$ampm = 'pm';
}
if ($hour==12) {
$ampm = 'pm';
}
endfor;
}
add_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );
Go figure.