{"id":1350,"date":"2019-03-11T10:37:12","date_gmt":"2019-03-11T20:37:12","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=1335"},"modified":"2019-03-11T10:37:12","modified_gmt":"2019-03-11T20:37:12","slug":"php-7-2-and-the-502-error","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/","title":{"rendered":"PHP 7.2 and the 502 error"},"content":{"rendered":"\n
My site was running fine and dandy for a few years.\u00a0 I made the necessary adjustments to make it compatible with PHP 7.2 and upgraded the server.\u00a0 A few days later I started receiving random 502 errors.<\/p>\n\n\n\n
The problem apparently was a simple else if statement within a WordPress filter.\u00a0 Here is the important parts of the code:<\/p>\n\n\n\n
function php72_killer() {\n for($hour = 8; $hour < 17; $hour++) :\n $display_hour = $hour;\n $ampm = 'am';\n if ( $hour == 0 ) {\n $display_hour = 12;\n }\n else if ( $hour > 12 ) {\n $display_hour = $hour - 12;\n $ampm = 'pm';\n }\n else if ($hour==12) {\n $ampm = 'pm';\n }\n endfor;\n}\nadd_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );<\/code><\/pre>\n\n\n\nThat last else if turned out to be the problem?\u00a0 Why?\u00a0 I don’t fully understand, but I found PHP 7.2 introduced optimized else ifs:\u00a0\u00a0https:\/\/derickrethans.nl\/php7.2-switch.html<\/a><\/p>\n\n\n\nIt’s important to note that this for loop with if-statements outside of the WordPress callback doesn’t cause random 502 errors.<\/p>\n\n\n\n
So what fixed my 502 errors?\u00a0 not having a 2nd else if:<\/p>\n\n\n\n
function php72_killer() {\n for($hour = 8; $hour < 17; $hour++) :\n $display_hour = $hour;\n $ampm = 'am';\n if ( $hour == 0 ) {\n $display_hour = 12;\n }\n else if ( $hour > 12 ) {\n $display_hour = $hour - 12;\n $ampm = 'pm';\n }\n if ($hour==12) {\n $ampm = 'pm';\n }\n endfor;\n}\nadd_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );<\/code><\/pre>\n\n\n\nGo figure.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2,6],"tags":[8,54,56],"yoast_head":"\n
PHP 7.2 and the 502 error - My Monkey Do<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n