3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getColor($rgb) { $colors = array(BLUE =>0x0a9ef3, RED => 0xea0a2f, GREEN => 0x336633); $largestDiff = 0; $closestColor = ""; foreach ($colors as $name => $rgbColor) { if (!isset($smallestDiff)) { $smallestDiff = colorDiff($rgbColor,$rgb); $closestColor = $name; } else if (colorDiff($rgbColor,$rgb) < $smallestDiff) { $smallestDiff = colorDiff($rgbColor,$rgb); $closestColor = $name; } } return $closestColor; } function colorDiff($rgb1,$rgb2) { // do the math on each tuple $red1 = hexdec(substr($rgb1,0,2)); $green1 = hexdec(substr($rgb1,2,2)); $blue1 = hexdec(substr($rgb1,4,2)); $red2 = hexdec(substr($rgb2,0,2)); $green2 = hexdec(substr($rgb2,2,2)); $blue2 = hexdec(substr($rgb2,4,2)); return abs($red1 - $red2) + abs($green1 - $green2) + abs($blue1 - $blue2) ; } $color = '336633'; $closestmatch = getColor($color); echo $closematch; ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4UXYT
function name:  (null)
number of ops:  7
compiled vars:  !0 = $color, !1 = $closestmatch, !2 = $closematch
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   ASSIGN                                                   !0, '336633'
   46     1        INIT_FCALL                                               'getcolor'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !1, $4
   47     5        ECHO                                                     !2
   52     6      > RETURN                                                   1

Function getcolor:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 36
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 36
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 35
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/4UXYT
function name:  getColor
number of ops:  39
compiled vars:  !0 = $rgb, !1 = $colors, !2 = $largestDiff, !3 = $closestColor, !4 = $rgbColor, !5 = $name, !6 = $smallestDiff
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    6     1        FETCH_CONSTANT                                   ~7      'BLUE'
          2        INIT_ARRAY                                       ~8      696051, ~7
          3        FETCH_CONSTANT                                   ~9      'RED'
          4        ADD_ARRAY_ELEMENT                                ~8      15338031, ~9
          5        FETCH_CONSTANT                                   ~10     'GREEN'
          6        ADD_ARRAY_ELEMENT                                ~8      3368499, ~10
          7        ASSIGN                                                   !1, ~8
    8     8        ASSIGN                                                   !2, 0
    9     9        ASSIGN                                                   !3, ''
   10    10      > FE_RESET_R                                       $14     !1, ->36
         11    > > FE_FETCH_R                                       ~15     $14, !4, ->36
         12    >   ASSIGN                                                   !5, ~15
   12    13        ISSET_ISEMPTY_CV                                 ~17     !6
         14        BOOL_NOT                                         ~18     ~17
         15      > JMPZ                                                     ~18, ->23
   14    16    >   INIT_FCALL_BY_NAME                                       'colorDiff'
         17        SEND_VAR_EX                                              !4
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0  $19     
         20        ASSIGN                                                   !6, $19
   15    21        ASSIGN                                                   !3, !5
         22      > JMP                                                      ->35
   17    23    >   INIT_FCALL_BY_NAME                                       'colorDiff'
         24        SEND_VAR_EX                                              !4
         25        SEND_VAR_EX                                              !0
         26        DO_FCALL                                      0  $22     
         27        IS_SMALLER                                               $22, !6
         28      > JMPZ                                                     ~23, ->35
   19    29    >   INIT_FCALL_BY_NAME                                       'colorDiff'
         30        SEND_VAR_EX                                              !4
         31        SEND_VAR_EX                                              !0
         32        DO_FCALL                                      0  $24     
         33        ASSIGN                                                   !6, $24
   20    34        ASSIGN                                                   !3, !5
   10    35    > > JMP                                                      ->11
         36    >   FE_FREE                                                  $14
   24    37      > RETURN                                                   !3
   26    38*     > RETURN                                                   null

End of function getcolor

Function colordiff:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4UXYT
function name:  colorDiff
number of ops:  72
compiled vars:  !0 = $rgb1, !1 = $rgb2, !2 = $red1, !3 = $green1, !4 = $blue1, !5 = $red2, !6 = $green2, !7 = $blue2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        INIT_FCALL                                               'hexdec'
          3        INIT_FCALL                                               'substr'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 0
          6        SEND_VAL                                                 2
          7        DO_ICALL                                         $8      
          8        SEND_VAR                                                 $8
          9        DO_ICALL                                         $9      
         10        ASSIGN                                                   !2, $9
   33    11        INIT_FCALL                                               'hexdec'
         12        INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 2
         15        SEND_VAL                                                 2
         16        DO_ICALL                                         $11     
         17        SEND_VAR                                                 $11
         18        DO_ICALL                                         $12     
         19        ASSIGN                                                   !3, $12
   34    20        INIT_FCALL                                               'hexdec'
         21        INIT_FCALL                                               'substr'
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 4
         24        SEND_VAL                                                 2
         25        DO_ICALL                                         $14     
         26        SEND_VAR                                                 $14
         27        DO_ICALL                                         $15     
         28        ASSIGN                                                   !4, $15
   36    29        INIT_FCALL                                               'hexdec'
         30        INIT_FCALL                                               'substr'
         31        SEND_VAR                                                 !1
         32        SEND_VAL                                                 0
         33        SEND_VAL                                                 2
         34        DO_ICALL                                         $17     
         35        SEND_VAR                                                 $17
         36        DO_ICALL                                         $18     
         37        ASSIGN                                                   !5, $18
   37    38        INIT_FCALL                                               'hexdec'
         39        INIT_FCALL                                               'substr'
         40        SEND_VAR                                                 !1
         41        SEND_VAL                                                 2
         42        SEND_VAL                                                 2
         43        DO_ICALL                                         $20     
         44        SEND_VAR                                                 $20
         45        DO_ICALL                                         $21     
         46        ASSIGN                                                   !6, $21
   38    47        INIT_FCALL                                               'hexdec'
         48        INIT_FCALL                                               'substr'
         49        SEND_VAR                                                 !1
         50        SEND_VAL                                                 4
         51        SEND_VAL                                                 2
         52        DO_ICALL                                         $23     
         53        SEND_VAR                                                 $23
         54        DO_ICALL                                         $24     
         55        ASSIGN                                                   !7, $24
   40    56        INIT_FCALL                                               'abs'
         57        SUB                                              ~26     !2, !5
         58        SEND_VAL                                                 ~26
         59        DO_ICALL                                         $27     
         60        INIT_FCALL                                               'abs'
         61        SUB                                              ~28     !3, !6
         62        SEND_VAL                                                 ~28
         63        DO_ICALL                                         $29     
         64        ADD                                              ~30     $27, $29
         65        INIT_FCALL                                               'abs'
         66        SUB                                              ~31     !4, !7
         67        SEND_VAL                                                 ~31
         68        DO_ICALL                                         $32     
         69        ADD                                              ~33     ~30, $32
         70      > RETURN                                                   ~33
   43    71*     > RETURN                                                   null

End of function colordiff

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.05 ms | 1407 KiB | 20 Q