3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Card { private $suit; private $val; public function __construct($val, $suit) { $values = array( '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10, 'J' => 11, 'Q' => 12, 'K' => 13, 'A' => 14, ); $suits = array( 'H' => 'Hearts', 'C' => 'Clubs', 'D' => 'Diamonds', 'S' => 'Spades' ); $this->suit = $suits[$suit]; $this->val = $values[$val]; } public function getSuit() { return $this->suit; } public function getVal() { return $this->val; } } class Hand { private $cards; private $matches = array(); public function __construct($hand) { $cards = explode(' ', $hand); foreach ($cards as $card) { $val = substr($card, 0, -1); $suit = strtoupper(substr($card, -1)); $this->cards[] = new Card($val, $suit); } $this->isPair(); $this->isHighCard(); $this->isKind(); $this->isStraight(); } public function getCards(){ return $this->cards; } public function getValues(){ foreach($this->cards as $card){ $output[] = $card->getVal(); } return $output; } public function isHighCard(){ $output = max($this->getValues()); return $output; } public function isPair(){ foreach(array_count_values($this->getValues()) as $value => $count){ if ($count == 2 && count($this->matches) < 2) { $this->matches[] = $value; } } if (count($this->matches) == 1) { echo "One pair"; } if (count($this->matches) == 2) { echo "Two pair"; } } public function isKind(){ $count = max(array_count_values($this->getValues())); if($count == 3){ echo "Three of a kind"; } if($count == 4){ echo "Four of a kind"; } } public function isStraight(){ $cards = $this->getValues(); $previous = null; foreach($cards as $card){ if ($previous !== null && $card == $previous + 1){ echo "Straight"; } $previous = $card; } } } $h = new Hand('6h 7d 8s 9c 10h'); // var_dump($x);
based on T5kHX
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EfNtm
function name:  (null)
number of ops:  5
compiled vars:  !0 = $h
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
 131     0  E >   NEW                                              $1      :5
         1        SEND_VAL_EX                                              '6h+7d+8s+9c+10h'
         2        DO_FCALL                                      0          
         3        QM_ASSIGN                                                !0
 133     4      > RETURN                                                   1

Class Card:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EfNtm
function name:  __construct
number of ops:  11
compiled vars:  !0 = $val, !1 = $suit, !2 = $values, !3 = $suits
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   8     0  E >   RECV                                             !0      
         1        RECV                                             !1      
  11     2        QM_ASSIGN                                                !2
  27     3        QM_ASSIGN                                                !3
  33     4        FETCH_DIM_R                                      ~7      !3, !1
         5        ASSIGN_SUB                                    0          'suit'
         6        OP_DATA                                                  ~7
  34     7        FETCH_DIM_R                                      ~9      !2, !0
         8        ASSIGN_SUB                                   24          'val'
         9        OP_DATA                                                  ~9
  35    10      > RETURN                                                   null

End of function __construct

Function getsuit:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EfNtm
function name:  getSuit
number of ops:  3
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  38     0  E >   FETCH_OBJ_R                                      ~0      'suit'
         1      > RETURN                                                   ~0
  39     2*     > RETURN                                                   null

End of function getsuit

Function getval:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EfNtm
function name:  getVal
number of ops:  3
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  42     0  E >   FETCH_OBJ_R                                      ~0      'val'
         1      > RETURN                                                   ~0
  43     2*     > RETURN                                                   null

End of function getval

End of class Card.

Class Hand:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 30
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 30
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/EfNtm
function name:  __construct
number of ops:  40
compiled vars:  !0 = $hand, !1 = $cards, !2 = $card, !3 = $val, !4 = $suit
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  53     0  E >   RECV                                             !0      
  54     1        INIT_FCALL                                               'explode'
         2        SEND_VAL                                                 '+'
         3        SEND_VAR                                                 !0
         4        DO_ICALL                                         $5      
         5        QM_ASSIGN                                                !1
  56     6      > FE_RESET_R                                       $7      !1, ->30
         7    > > FE_FETCH_R                                               $7, !2, ->30
  57     8    >   INIT_FCALL                                               'substr'
         9        SEND_VAR                                                 !2
        10        SEND_VAL                                                 0
        11        SEND_VAL                                                 -1
        12        DO_ICALL                                         $8      
        13        QM_ASSIGN                                                !3
  58    14        INIT_FCALL                                               'strtoupper'
        15        INIT_FCALL                                               'substr'
        16        SEND_VAR                                                 !2
        17        SEND_VAL                                                 -1
        18        DO_ICALL                                         $10     
        19        SEND_VAR                                                 $10
        20        DO_ICALL                                         $11     
        21        QM_ASSIGN                                                !4
  59    22        NEW                                              $15     :40
        23        SEND_VAR_EX                                              !3
        24        SEND_VAR_EX                                              !4
        25        DO_FCALL                                      0          
        26        FETCH_OBJ_W                                      $13     'cards'
        27        ASSIGN_ADD                                    0          $13
        28        OP_DATA                                                  $15
  56    29      > JMP                                                      ->7
        30    >   FE_FREE                                                  $7
  63    31        INIT_METHOD_CALL                                         'isPair'
        32        DO_FCALL                                      0          
  64    33        INIT_METHOD_CALL                                         'isHighCard'
        34        DO_FCALL                                      0          
  65    35        INIT_METHOD_CALL                                         'isKind'
        36        DO_FCALL                                      0          
  66    37        INIT_METHOD_CALL                                         'isStraight'
        38        DO_FCALL                                      0          
  67    39      > RETURN                                                   null

End of function __construct

Function getcards:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EfNtm
function name:  getCards
number of ops:  3
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  70     0  E >   FETCH_OBJ_R                                      ~0      'cards'
         1      > RETURN                                                   ~0
  71     2*     > RETURN                                                   null

End of function getcards

Function getvalues:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/EfNtm
function name:  getValues
number of ops:  11
compiled vars:  !0 = $card, !1 = $output
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  75     0  E >   FETCH_OBJ_R                                      ~2      'cards'
         1      > FE_RESET_R                                       $3      ~2, ->8
         2    > > FE_FETCH_R                                               $3, !0, ->8
  76     3    >   INIT_METHOD_CALL                                         !0, 'getVal'
         4        DO_FCALL                                      0  $5      
         5        ASSIGN_ADD                                    0          !1
         6        OP_DATA                                                  $5
  75     7      > JMP                                                      ->2
         8    >   FE_FREE                                                  $3
  78     9      > RETURN                                                   !1
  79    10*     > RETURN                                                   null

End of function getvalues

Function ishighcard:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EfNtm
function name:  isHighCard
number of ops:  8
compiled vars:  !0 = $output
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  82     0  E >   INIT_FCALL                                               'max'
         1        INIT_METHOD_CALL                                         'getValues'
         2        DO_FCALL                                      0  $1      
         3        SEND_VAR                                                 $1
         4        DO_ICALL                                         $2      
         5        QM_ASSIGN                                                !0
  83     6      > RETURN                                                   !0
  84     7*     > RETURN                                                   null

End of function ishighcard

Function ispair:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 19
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 19
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 18
Branch analysis from position: 14
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 25
Branch analysis from position: 19
filename:       /in/EfNtm
function name:  isPair
number of ops:  31
compiled vars:  !0 = $count, !1 = $value
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  87     0  E >   INIT_FCALL                                               'array_count_values'
         1        INIT_METHOD_CALL                                         'getValues'
         2        DO_FCALL                                      0  $2      
         3        SEND_VAR                                                 $2
         4        DO_ICALL                                         $3      
         5      > FE_RESET_R                                       $4      $3, ->19
         6    > > FE_FETCH_R                                       ~5      $4, !0, ->19
         7    >   QM_ASSIGN                                                !1
  88     8        IS_NOT_EQUAL                                     ~7      !0, 2
         9      > JMPZ_EX                                          ~7      ~7, ->14
        10    >   FETCH_OBJ_R                                      ~8      'matches'
        11        COUNT                                            ~9      ~8
        12        IS_SMALLER_OR_EQUAL                              ~10     ~9, 2
        13        BOOL                                             ~7      ~10
        14    > > JMPZ                                                     ~7, ->18
  89    15    >   FETCH_OBJ_W                                      $11     'matches'
        16        ASSIGN_ADD                                    0          $11
        17        OP_DATA                                                  !1
  87    18    > > JMP                                                      ->6
        19    >   FE_FREE                                                  $4
  94    20        FETCH_OBJ_R                                      ~13     'matches'
        21        COUNT                                            ~14     ~13
        22        IS_NOT_EQUAL                                     ~15     ~14, 1
        23      > JMPZ                                                     ~15, ->25
  95    24    >   ASSIGN_OBJ                                               'One+pair'
  97    25    >   FETCH_OBJ_R                                      ~16     'matches'
        26        COUNT                                            ~17     ~16
        27        IS_NOT_EQUAL                                     ~18     ~17, 2
        28      > JMPZ                                                     ~18, ->30
  98    29    >   ASSIGN_OBJ                                               'Two+pair'
 101    30    > > RETURN                                                   null

End of function ispair

Function iskind:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 12
filename:       /in/EfNtm
function name:  isKind
number of ops:  16
compiled vars:  !0 = $count
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
 104     0  E >   INIT_FCALL                                               'max'
         1        INIT_FCALL                                               'array_count_values'
         2        INIT_METHOD_CALL                                         'getValues'
         3        DO_FCALL                                      0  $1      
         4        SEND_VAR                                                 $1
         5        DO_ICALL                                         $2      
         6        SEND_VAR                                                 $2
         7        DO_ICALL                                         $3      
         8        QM_ASSIGN                                                !0
 106     9        IS_NOT_EQUAL                                     ~5      !0, 3
        10      > JMPZ                                                     ~5, ->12
 107    11    >   ASSIGN_OBJ                                               'Three+of+a+kind'
 109    12    >   IS_NOT_EQUAL                                     ~6      !0, 4
        13      > JMPZ                                                     ~6, ->15
 110    14    >   ASSIGN_OBJ                                               'Four+of+a+kind'
 112    15    > > RETURN                                                   null

End of function iskind

Function isstraight:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
Branch analysis from position: 11
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/EfNtm
function name:  isStraight
number of ops:  17
compiled vars:  !0 = $cards, !1 = $previous, !2 = $card
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
 115     0  E >   INIT_METHOD_CALL                                         'getValues'
         1        DO_FCALL                                      0  $3      
         2        QM_ASSIGN                                                !0
 116     3        QM_ASSIGN                                                !1
 117     4      > FE_RESET_R                                       $6      !0, ->15
         5    > > FE_FETCH_R                                               $6, !2, ->15
 118     6    >   IS_EQUAL                                         ~7      !1, null
         7      > JMPZ_EX                                          ~7      ~7, ->11
         8    >   ADD                                              ~8      !1, 1
         9        IS_NOT_EQUAL                                     ~9      !2, ~8
        10        BOOL                                             ~7      ~9
        11    > > JMPZ                                                     ~7, ->13
 119    12    >   ASSIGN_OBJ                                               'Straight'
 121    13    >   QM_ASSIGN                                                !1
 117    14      > JMP                                                      ->5
        15    >   FE_FREE                                                  $6
 124    16      > RETURN                                                   null

End of function isstraight

End of class Hand.

Generated using Vulcan Logic Dumper, using php 7.4.0