3v4l.org

run code in 300+ PHP versions simultaneously
<?php $allowedweapons = ['Scissors', 'Paper', 'Rock', 'Lizard', 'Spock']; $rules = [ 'scissorspaper' => 'cuts', 'paperrock' => 'covers', 'rocklizard' => 'crushes', 'lizardspock' => 'poisons', 'spockscissors' => 'smashes', 'scissorslizard' => 'decapitates', 'lizardpaper' => 'eats', 'paperspock' => 'disproves', 'spockrock' => 'vaporizes', 'rockscissors' => 'crushes' ]; class Game { protected $rules; public function __construct($rules) { $this->rules = $rules; } public function run($weaponP1, $weaponP2) { if ($weaponP1 === $weaponP2) { echo "It is a tie"; return; } $result = $this->checkVerbs($weaponP1, $weaponP2); $winner = 'Player 1'; if (!$result) { $result = $this->checkVerbs($weaponP2, $weaponP1); $winner = 'Player 2'; } echo "The winner is: $winner\n"; echo "Because of: " . $result; } /** * @param $weaponP1 * @param $weaponP2 * @return bool|string */ protected function checkVerbs($weaponP1, $weaponP2) { $key = strtolower($weaponP1 . $weaponP2); if (!array_key_exists($key, $this->rules)) { return false; } return $weaponP1 . ' ' . $this->rules[$key] . ' ' . $weaponP2; } } (new Game($rules))->run('Scissors', 'Paper');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oom3P
function name:  (null)
number of ops:  10
compiled vars:  !0 = $allowedweapons, !1 = $rules
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
    4     1        ASSIGN                                                   !1, <array>
   59     2        NEW                                              $4      'Game'
          3        SEND_VAR_EX                                              !1
          4        DO_FCALL                                      0          
          5        INIT_METHOD_CALL                                         $4, 'run'
          6        SEND_VAL_EX                                              'Scissors'
          7        SEND_VAL_EX                                              'Paper'
          8        DO_FCALL                                      0          
          9      > RETURN                                                   1

Class Game:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oom3P
function name:  __construct
number of ops:  4
compiled vars:  !0 = $rules
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        ASSIGN_OBJ                                               'rules'
          2        OP_DATA                                                  !0
   24     3      > RETURN                                                   null

End of function __construct

Function run:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/oom3P
function name:  run
number of ops:  27
compiled vars:  !0 = $weaponP1, !1 = $weaponP2, !2 = $result, !3 = $winner
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        IS_IDENTICAL                                             !0, !1
          3      > JMPZ                                                     ~4, ->6
   29     4    >   ECHO                                                     'It+is+a+tie'
   30     5      > RETURN                                                   null
   33     6    >   INIT_METHOD_CALL                                         'checkVerbs'
          7        SEND_VAR_EX                                              !0
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0  $5      
         10        ASSIGN                                                   !2, $5
   34    11        ASSIGN                                                   !3, 'Player+1'
   35    12        BOOL_NOT                                         ~8      !2
         13      > JMPZ                                                     ~8, ->20
   36    14    >   INIT_METHOD_CALL                                         'checkVerbs'
         15        SEND_VAR_EX                                              !1
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0  $9      
         18        ASSIGN                                                   !2, $9
   37    19        ASSIGN                                                   !3, 'Player+2'
   40    20    >   ROPE_INIT                                     3  ~13     'The+winner+is%3A+'
         21        ROPE_ADD                                      1  ~13     ~13, !3
         22        ROPE_END                                      2  ~12     ~13, '%0A'
         23        ECHO                                                     ~12
   41    24        CONCAT                                           ~15     'Because+of%3A+', !2
         25        ECHO                                                     ~15
   42    26      > RETURN                                                   null

End of function run

Function checkverbs:
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
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oom3P
function name:  checkVerbs
number of ops:  20
compiled vars:  !0 = $weaponP1, !1 = $weaponP2, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   51     2        INIT_FCALL                                               'strtolower'
          3        CONCAT                                           ~3      !0, !1
          4        SEND_VAL                                                 ~3
          5        DO_ICALL                                         $4      
          6        ASSIGN                                                   !2, $4
   52     7        FETCH_OBJ_R                                      ~6      'rules'
          8        ARRAY_KEY_EXISTS                                 ~7      !2, ~6
          9        BOOL_NOT                                         ~8      ~7
         10      > JMPZ                                                     ~8, ->12
   53    11    > > RETURN                                                   <false>
   55    12    >   CONCAT                                           ~9      !0, '+'
         13        FETCH_OBJ_R                                      ~10     'rules'
         14        FETCH_DIM_R                                      ~11     ~10, !2
         15        CONCAT                                           ~12     ~9, ~11
         16        CONCAT                                           ~13     ~12, '+'
         17        CONCAT                                           ~14     ~13, !1
         18      > RETURN                                                   ~14
   56    19*     > RETURN                                                   null

End of function checkverbs

End of class Game.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.42 ms | 1400 KiB | 15 Q