{"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\n

That 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\n

It’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\n

Go 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":"\nPHP 7.2 and the 502 error - My Monkey Do<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP 7.2 and the 502 error - My Monkey Do\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/\" \/>\n<meta property=\"og:site_name\" content=\"My Monkey Do\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-11T20:37:12+00:00\" \/>\n<meta name=\"author\" content=\"Webhead\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Webhead\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/\",\"url\":\"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/\",\"name\":\"PHP 7.2 and the 502 error - My Monkey Do\",\"isPartOf\":{\"@id\":\"https:\/\/mymonkeydo.com\/#website\"},\"datePublished\":\"2019-03-11T20:37:12+00:00\",\"dateModified\":\"2019-03-11T20:37:12+00:00\",\"author\":{\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\"},\"breadcrumb\":{\"@id\":\"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mymonkeydo.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP 7.2 and the 502 error\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mymonkeydo.com\/#website\",\"url\":\"https:\/\/mymonkeydo.com\/\",\"name\":\"My Monkey Do\",\"description\":\"A Log of Coding Solutions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mymonkeydo.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\",\"name\":\"Webhead\",\"url\":\"https:\/\/mymonkeydo.com\/author\/corey\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP 7.2 and the 502 error - My Monkey Do","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/","og_locale":"en_US","og_type":"article","og_title":"PHP 7.2 and the 502 error - My Monkey Do","og_url":"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/","og_site_name":"My Monkey Do","article_published_time":"2019-03-11T20:37:12+00:00","author":"Webhead","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Webhead","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/","url":"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/","name":"PHP 7.2 and the 502 error - My Monkey Do","isPartOf":{"@id":"https:\/\/mymonkeydo.com\/#website"},"datePublished":"2019-03-11T20:37:12+00:00","dateModified":"2019-03-11T20:37:12+00:00","author":{"@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829"},"breadcrumb":{"@id":"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mymonkeydo.com\/php-7-2-and-the-502-error\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mymonkeydo.com\/"},{"@type":"ListItem","position":2,"name":"PHP 7.2 and the 502 error"}]},{"@type":"WebSite","@id":"https:\/\/mymonkeydo.com\/#website","url":"https:\/\/mymonkeydo.com\/","name":"My Monkey Do","description":"A Log of Coding Solutions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mymonkeydo.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829","name":"Webhead","url":"https:\/\/mymonkeydo.com\/author\/corey\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/1350"}],"collection":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/comments?post=1350"}],"version-history":[{"count":0,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/1350\/revisions"}],"wp:attachment":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/media?parent=1350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/categories?post=1350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/tags?post=1350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}