3v4l.org

run code in 300+ PHP versions simultaneously
<?php class UnitConverter { public $vulgar_to_float = ['½' => 0.5]; public function replaceUnicode( $amount = 0 ){ $half = $this->vulgar_to_float['½']; return ( is_numeric( $amount ) )? $amount : $half; } public function convertAmount($amount, $from, $to){ if($from === 'pound' && $to === 'ounce'){ return $amount * 16; } else if($from === 'cup' && $to === 'tablespoon'){ return $this->replaceUnicode( $amount ) * 16; } else { throw new Exception('Unable to convert ' . $from . ' to ' . $to); } } } function convertIngredients( $arr = null){ $unit_names = array_column($arr, 'unit_name'); foreach($unit_names as $key => $unit){ if( $unit === 'pound'){ $arr[$key]['amount'] = (new UnitConverter())->convertAmount($arr[$key]['amount'], $unit, 'ounce'); $arr[$key]['unit_name'] = 'ounce'; } else if($unit === 'cup'){ $arr[$key]['amount'] = (new UnitConverter())->convertAmount($arr[$key]['amount'], $unit, 'tablespoon'); $arr[$key]['unit_name'] = 'tablespoon'; } else { continue; } } return $arr; } function generateBreakfast(){ $array = [ 0 => array( 'amount' => 1, 'unit_name' => 'cup', 'ingredient_name' => 'almond flour' ), 1 => array( 'amount' => '½', 'unit_name' => 'cup', 'ingredient_name' => 'erythritol' ), 2 => array( 'amount' => 1, 'unit_name' => 'large', 'ingredient_name' => 'egg' ) ]; return convertIngredients($array); } echo '<pre>'; print_r(generateBreakfast()); echo '</pre>';
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vd4DM
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   ECHO                                                     '%3Cpre%3E'
   53     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'generatebreakfast'
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
   54     6        ECHO                                                     '%3C%2Fpre%3E'
          7      > RETURN                                                   1

Function convertingredients:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 49
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 49
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 28
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 47
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
filename:       /in/vd4DM
function name:  convertIngredients
number of ops:  52
compiled vars:  !0 = $arr, !1 = $unit_names, !2 = $unit, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV_INIT                                        !0      null
   26     1        INIT_FCALL                                               'array_column'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'unit_name'
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   27     6      > FE_RESET_R                                       $6      !1, ->49
          7    > > FE_FETCH_R                                       ~7      $6, !2, ->49
          8    >   ASSIGN                                                   !3, ~7
   28     9        IS_IDENTICAL                                             !2, 'pound'
         10      > JMPZ                                                     ~9, ->28
   29    11    >   NEW                                              $12     'UnitConverter'
         12        DO_FCALL                                      0          
         13        INIT_METHOD_CALL                                         $12, 'convertAmount'
         14        CHECK_FUNC_ARG                                           
         15        FETCH_DIM_FUNC_ARG                               $14     !0, !3
         16        FETCH_DIM_FUNC_ARG                               $15     $14, 'amount'
         17        SEND_FUNC_ARG                                            $15
         18        SEND_VAR_EX                                              !2
         19        SEND_VAL_EX                                              'ounce'
         20        DO_FCALL                                      0  $16     
         21        FETCH_DIM_W                                      $10     !0, !3
         22        ASSIGN_DIM                                               $10, 'amount'
         23        OP_DATA                                                  $16
   30    24        FETCH_DIM_W                                      $17     !0, !3
         25        ASSIGN_DIM                                               $17, 'unit_name'
         26        OP_DATA                                                  'ounce'
   28    27      > JMP                                                      ->48
   31    28    >   IS_IDENTICAL                                             !2, 'cup'
         29      > JMPZ                                                     ~19, ->47
   32    30    >   NEW                                              $22     'UnitConverter'
         31        DO_FCALL                                      0          
         32        INIT_METHOD_CALL                                         $22, 'convertAmount'
         33        CHECK_FUNC_ARG                                           
         34        FETCH_DIM_FUNC_ARG                               $24     !0, !3
         35        FETCH_DIM_FUNC_ARG                               $25     $24, 'amount'
         36        SEND_FUNC_ARG                                            $25
         37        SEND_VAR_EX                                              !2
         38        SEND_VAL_EX                                              'tablespoon'
         39        DO_FCALL                                      0  $26     
         40        FETCH_DIM_W                                      $20     !0, !3
         41        ASSIGN_DIM                                               $20, 'amount'
         42        OP_DATA                                                  $26
   33    43        FETCH_DIM_W                                      $27     !0, !3
         44        ASSIGN_DIM                                               $27, 'unit_name'
         45        OP_DATA                                                  'tablespoon'
   31    46      > JMP                                                      ->48
   35    47    > > JMP                                                      ->7
   27    48    > > JMP                                                      ->7
         49    >   FE_FREE                                                  $6
   39    50      > RETURN                                                   !0
   40    51*     > RETURN                                                   null

End of function convertingredients

Function generatebreakfast:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vd4DM
function name:  generateBreakfast
number of ops:  6
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   ASSIGN                                                   !0, <array>
   49     1        INIT_FCALL                                               'convertingredients'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4      > RETURN                                                   $2
   50     5*     > RETURN                                                   null

End of function generatebreakfast

Class UnitConverter:
Function replaceunicode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vd4DM
function name:  replaceUnicode
number of ops:  13
compiled vars:  !0 = $amount, !1 = $half
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV_INIT                                        !0      0
    8     1        FETCH_OBJ_R                                      ~2      'vulgar_to_float'
          2        FETCH_DIM_R                                      ~3      ~2, '%C2%BD'
          3        ASSIGN                                                   !1, ~3
    9     4        INIT_FCALL                                               'is_numeric'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $5      
          7      > JMPZ                                                     $5, ->10
          8    >   QM_ASSIGN                                        ~6      !0
          9      > JMP                                                      ->11
         10    >   QM_ASSIGN                                        ~6      !1
         11    > > RETURN                                                   ~6
   10    12*     > RETURN                                                   null

End of function replaceunicode

Function convertamount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 22
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 7
filename:       /in/vd4DM
function name:  convertAmount
number of ops:  30
compiled vars:  !0 = $amount, !1 = $from, !2 = $to
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   14     3        IS_IDENTICAL                                     ~3      !1, 'pound'
          4      > JMPZ_EX                                          ~3      ~3, ->7
          5    >   IS_IDENTICAL                                     ~4      !2, 'ounce'
          6        BOOL                                             ~3      ~4
          7    > > JMPZ                                                     ~3, ->11
   15     8    >   MUL                                              ~5      !0, 16
          9      > RETURN                                                   ~5
   14    10*       JMP                                                      ->29
   16    11    >   IS_IDENTICAL                                     ~6      !1, 'cup'
         12      > JMPZ_EX                                          ~6      ~6, ->15
         13    >   IS_IDENTICAL                                     ~7      !2, 'tablespoon'
         14        BOOL                                             ~6      ~7
         15    > > JMPZ                                                     ~6, ->22
   17    16    >   INIT_METHOD_CALL                                         'replaceUnicode'
         17        SEND_VAR_EX                                              !0
         18        DO_FCALL                                      0  $8      
         19        MUL                                              ~9      $8, 16
         20      > RETURN                                                   ~9
   16    21*       JMP                                                      ->29
   19    22    >   NEW                                              $10     'Exception'
         23        CONCAT                                           ~11     'Unable+to+convert+', !1
         24        CONCAT                                           ~12     ~11, '+to+'
         25        CONCAT                                           ~13     ~12, !2
         26        SEND_VAL_EX                                              ~13
         27        DO_FCALL                                      0          
         28      > THROW                                         0          $10
   21    29*     > RETURN                                                   null

End of function convertamount

End of class UnitConverter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.45 ms | 1022 KiB | 18 Q