3v4l.org

run code in 300+ PHP versions simultaneously
<?php function svg_icon(array $atts = []): ?string { // Get the provided values or a default value $icon = $atts['icon'] ?? false; $label = $atts['label'] ?? false; // This is required, so if it not set bail if (!$icon) { return null; } // These are our possible SVGs static $config = [ 'arrow' => '<svg aria-hidden="true" role="img" focusable="false" width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd"><path d="M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z"/></svg>', 'hamburger' => '<svg aria-hidden="true" role="img" focusable="false" clip-rule="evenodd" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m13 16.745c0-.414-.336-.75-.75-.75h-9.5c-.414 0-.75.336-.75.75s.336.75.75.75h9.5c.414 0 .75-.336.75-.75zm9-5c0-.414-.336-.75-.75-.75h-18.5c-.414 0-.75.336-.75.75s.336.75.75.75h18.5c.414 0 .75-.336.75-.75zm-4-5c0-.414-.336-.75-.75-.75h-14.5c-.414 0-.75.336-.75.75s.336.75.75.75h14.5c.414 0 .75-.336.75-.75z" fill-rule="nonzero"/></svg>', 'close' => '<svg aria-hidden="true" role="img" focusable="false" clip-rule="evenodd" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m12.002 2.005c5.518 0 9.998 4.48 9.998 9.997 0 5.518-4.48 9.998-9.998 9.998-5.517 0-9.997-4.48-9.997-9.998 0-5.517 4.48-9.997 9.997-9.997zm0 1.5c-4.69 0-8.497 3.807-8.497 8.497s3.807 8.498 8.497 8.498 8.498-3.808 8.498-8.498-3.808-8.497-8.498-8.497zm0 7.425 2.717-2.718c.146-.146.339-.219.531-.219.404 0 .75.325.75.75 0 .193-.073.384-.219.531l-2.717 2.717 2.727 2.728c.147.147.22.339.22.531 0 .427-.349.75-.75.75-.192 0-.384-.073-.53-.219l-2.729-2.728-2.728 2.728c-.146.146-.338.219-.53.219-.401 0-.751-.323-.751-.75 0-.192.073-.384.22-.531l2.728-2.728-2.722-2.722c-.146-.147-.219-.338-.219-.531 0-.425.346-.749.75-.749.192 0 .385.073.531.219z" fill-rule="nonzero"/></svg>', ]; // If the requested icon doesn't exist, bail if (!$svg = $config[$icon] ?? null) { return null; } // If we have a label, un-hide the SVG and set the ARIA attribute if (!empty($label)) { $svg = str_replace('<svg ', '<svg aria-label="'.esc_attr($label).'"', $svg); $svg = str_replace('aria-hidden="true"', '', $svg); } return $svg; } // Samples echo svg_icon(['icon' => 'hamburger', 'label' => 'Main Menu']); echo PHP_EOL; echo svg_icon(['icon' => 'arrow', 'label' => 'Click me']); echo PHP_EOL; echo svg_icon(['icon' => 'close']); echo PHP_EOL; echo svg_icon(['icon' => 'nope']); echo PHP_EOL; // !! DO NOT COPY THIS FUNCTION !! // This is a stub-function ONLY for the 3v4l.org site. function esc_attr($string){return $string;}
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HHWJKW
function name:  (null)
number of ops:  21
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   INIT_FCALL                                               'svg_icon'
          1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0  $0      
          3        ECHO                                                     $0
   39     4        ECHO                                                     '%0A'
   40     5        INIT_FCALL                                               'svg_icon'
          6        SEND_VAL                                                 <array>
          7        DO_FCALL                                      0  $1      
          8        ECHO                                                     $1
   41     9        ECHO                                                     '%0A'
   42    10        INIT_FCALL                                               'svg_icon'
         11        SEND_VAL                                                 <array>
         12        DO_FCALL                                      0  $2      
         13        ECHO                                                     $2
   43    14        ECHO                                                     '%0A'
   44    15        INIT_FCALL                                               'svg_icon'
         16        SEND_VAL                                                 <array>
         17        DO_FCALL                                      0  $3      
         18        ECHO                                                     $3
   45    19        ECHO                                                     '%0A'
   52    20      > RETURN                                                   1

Function svg_icon:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 40
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
filename:       /in/HHWJKW
function name:  svg_icon
number of ops:  44
compiled vars:  !0 = $atts, !1 = $icon, !2 = $label, !3 = $config, !4 = $svg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV_INIT                                        !0      <array>
    6     1        FETCH_DIM_IS                                     ~5      !0, 'icon'
          2        COALESCE                                         ~6      ~5
          3        QM_ASSIGN                                        ~6      <false>
          4        ASSIGN                                                   !1, ~6
    7     5        FETCH_DIM_IS                                     ~8      !0, 'label'
          6        COALESCE                                         ~9      ~8
          7        QM_ASSIGN                                        ~9      <false>
          8        ASSIGN                                                   !2, ~9
   10     9        BOOL_NOT                                         ~11     !1
         10      > JMPZ                                                     ~11, ->12
   11    11    > > RETURN                                                   null
   15    12    >   BIND_STATIC                                              !3
   22    13        FETCH_DIM_IS                                     ~12     !3, !1
         14        COALESCE                                         ~13     ~12
         15        QM_ASSIGN                                        ~13     null
         16        ASSIGN                                           ~14     !4, ~13
         17        BOOL_NOT                                         ~15     ~14
         18      > JMPZ                                                     ~15, ->20
   23    19    > > RETURN                                                   null
   27    20    >   ISSET_ISEMPTY_CV                                 ~16     !2
         21        BOOL_NOT                                         ~17     ~16
         22      > JMPZ                                                     ~17, ->40
   28    23    >   INIT_FCALL                                               'str_replace'
         24        SEND_VAL                                                 '%3Csvg+'
         25        INIT_FCALL_BY_NAME                                       'esc_attr'
         26        SEND_VAR_EX                                              !2
         27        DO_FCALL                                      0  $18     
         28        CONCAT                                           ~19     '%3Csvg+aria-label%3D%22', $18
         29        CONCAT                                           ~20     ~19, '%22'
         30        SEND_VAL                                                 ~20
         31        SEND_VAR                                                 !4
         32        DO_ICALL                                         $21     
         33        ASSIGN                                                   !4, $21
   29    34        INIT_FCALL                                               'str_replace'
         35        SEND_VAL                                                 'aria-hidden%3D%22true%22'
         36        SEND_VAL                                                 ''
         37        SEND_VAR                                                 !4
         38        DO_ICALL                                         $23     
         39        ASSIGN                                                   !4, $23
   32    40    >   VERIFY_RETURN_TYPE                                       !4
         41      > RETURN                                                   !4
   34    42*       VERIFY_RETURN_TYPE                                       
         43*     > RETURN                                                   null

End of function svg_icon

Function esc_attr:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HHWJKW
function name:  esc_attr
number of ops:  3
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1      > RETURN                                                   !0
          2*     > RETURN                                                   null

End of function esc_attr

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.91 ms | 1005 KiB | 18 Q