Advertisement
Guest User

funny bug with extract and string concat (WAMP)

a guest
Jul 2nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * funny bug with extract and string concat
  5.  * that results in ERR_CONNECTION_RESET (WAMP)
  6.  */
  7.  
  8. //custom styles I get from the CMS editor
  9. $json = '{
  10.     "classname": ".b-1067",
  11.     "styles": {
  12.         "only screen": {
  13.             "background": "#E6F0FA",
  14.             "padding-top": "4rem 0 2rem",
  15.             "margin-top": "0"
  16.         },
  17.         "only screen and (min-width: 720px)": []
  18.     },
  19.     "childStyles": {
  20.         "only screen": [
  21.             ".wrap { max-width: 1200px; }"
  22.         ],
  23.         "only screen and (min-width: 720px)": [
  24.             ".grid__item { width: 50%; }",
  25.             ".grid__item { border-width: 0 0.5rem 0.5rem; }"
  26.         ]
  27.     }
  28. }';
  29.  
  30. //renaming $styles to $data prevents the bug
  31. $styles = json_decode($json, true);
  32. extract($styles); //destruct this variable to give me $classname, $styles, $childStyles
  33.  
  34.  
  35. //this outer loop would be the different CMS content blocks
  36. //removing this loop prevents ERR_CONNECTION_RESET and reveals the error message (undefined index)
  37. $blocks = array(1,2,3,4,5,6,7,8,9);
  38. foreach($blocks as $block) {
  39.  
  40.     foreach($styles as $mq => $attributes) {  
  41.  
  42.         //echo main styles ...
  43.  
  44.         //echo nested child styles
  45.         foreach($childStyles[$mq] as $style) {
  46.             echo $classname .' '. $style; //ERR_CONNECTION_RESET (WAMP)
  47.            
  48.             //works:
  49.             //echo $classname;
  50.             //echo ' ';
  51.             //echo $style;
  52.  
  53.             echo '<hr />';
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement