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('9s 9h 9d 10s 8c'); // var_dump($x);

preferences:
73.44 ms | 402 KiB | 5 Q