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; 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->isFlush(); $this->isStraight(); $this->isFourKind(); $this->isThreeKind(); $this->isTwoPair(); $this->isOnePair(); $this->isHighCard(); } public function getCards(){ return $this->cards; } public function getValues(){ foreach($this->cards as $card){ $output[] = $card->getVal(); } return $output; } public function getSuits(){ foreach($this->cards as $card){ $output[] = $card->getSuit(); } return $output; } public function isHighCard(){ $output = max($this->getValues()); return $output; } public function isPair(){ $this->matches = array(); foreach(array_count_values($this->getValues()) as $value => $count){ if ($count == 2 && count($this->matches) < 2) { $this->matches[] = $value; } } } public function isOnePair(){ $this->isPair(); if (count($this->matches) == 1) { echo "One pair"; } } public function isTwoPair(){ $this->isPair(); if (count($this->matches) == 2) { echo "Two pair"; } } public function isThreeKind(){ $count = max(array_count_values($this->getValues())); if($count == 3){ echo "Three of a kind"; } } public function isFourKind(){ $count = max(array_count_values($this->getValues())); if($count == 4){ echo "Four of a kind"; } } public function isStraight(){ $min = min($this->getValues()); } public function isFlush(){ foreach (array_count_values($this->getSuits()) as $value => $count){ if ($count == 5) { echo "Flush"; } } } public function isStraightFlush(){ } } $h = new Hand('Kh Kc 3s 10h 2d'); // var_dump($x);

preferences:
65.36 ms | 402 KiB | 5 Q