3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Card { private $suit; private $val; public function __construct($val, $suit) { $values = array( '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] ?? $val; } public function getSuit() { return $this->suit; } public function getVal() { return $this->val; } public function setVal($val) { $this->val = $val; } } class Hand { private $cards; private $matches = array(); private $values = array(); private $flush; private $rank; 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); } // get values of cards in hand $this->values = array_map(function ($card) { return $card->getVal(); }, $this->cards); // get suits of cards in hand $this->suits = array_map(function ($card) { return $card->getSuit(); }, $this->cards); // get pairs of threes and fours $this->pairs = $this->threes = $this->fours = []; // might be a straight $this->possibleStraight = true; $this->hands = array_count_values($this->values); // if there are more than 2 same card values, it can't be a straight if (max($this->hands) > 1) { $this->possibleStraight = false; } // match for pairs foreach ($this->hands as $face => $count) { // one or two pairs if ($count === 2) { $this->pairs[] = $face; } // three of a kind if ($count === 3) { $this->threes[] = $face; } // four of a kind if ($count === 4) { $this->fours[] = $face; } } } public function evalHand() { $this->getHighCard(); $this->isOnePair(); $this->isTwoPair(); $this->getThrees(); $this->getFours(); $this->isFullHouse(); $this->isFlush(); $this->isStraight(); $this->isStraightFlush(); $this->isRoyalFlush(); } public function getRank() { echo $this->rank; } public function getCards() { return $this->cards; } public function getValues() { return $this->values; } public function getSuits() { return $this->suits; } public function getHighCard() { return max($this->getValues()); } public function getPairs() { return $this->pairs; } public function isOnePair() { if (count($this->pairs) == 1) { $this->rank = 'One Pair'; } return (count($this->pairs) == 1 ? true : false); } public function isTwoPair() { if (count($this->pairs) == 2) { $this->rank = 'Two Pair'; } return (count($this->pairs) == 2 ? true : false); } public function getThrees() { $this->rank = 'Three of a Kind'; return $this->threes; } public function getFours() { $this->rank = 'Four of a Kind'; return $this->fours; } public function hasAce() { return isset($this->hands['14']); } public function isFullHouse() { if (count($this->pairs) == 1 && count($this->threes) == 1) { $this->rank = 'Flush'; } return count($this->pairs) == 1 && count($this->threes) == 1; } public function isFlush() { $suits = array_count_values($this->getSuits()); // match flush foreach ($suits as $face => $count) { if ($count == 5) { $this->flush = true; $this->rank = 'Flush'; } else { $this->flush = false; } } return $this->flush; } public function isStraightAce() { $cards = $this->getValues(); if (array_search(14, $cards) !== FALSE && array_search(2, $cards) !== FALSE && array_search(3, $cards) !== FALSE && array_search(4, $cards) !== FALSE && array_search(5, $cards) !== FALSE) { $this->straight = true; } else { $this->straight = false; } return $this->straight; } public function isStraightRegular() { $cards = $this->getValues(); if (max($cards) - min($cards) == 4) { $this->straight = true; } else { $this->straight = false; } return $this->straight; } public function isStraight() { if ($this->isStraightAce()) { $this->straight = true; $this->rank = 'Straight'; } else if ($this->isStraightRegular()) { $this->straight = true; $this->rank = 'Straight'; } else { $this->straight = false; } return $this->straight; } public function isStraightFlush() { if ($this->isFlush() && $this->isStraight()) { $this->rank = 'Straight Flush'; }; return ($this->isFlush() && $this->isStraight()); } public function isRoyal() { $cards = $this->getValues(); if (array_search(10, $cards) !== FALSE && array_search(11, $cards) !== FALSE && array_search(12, $cards) !== FALSE && array_search(13, $cards) !== FALSE && array_search(14, $cards) !== FALSE) { return true; } else { return false; } } public function isRoyalFlush() { if ($this->isFlush() && $this->isRoyal()) { $this->rank = 'Royal Flush'; }; return ($this->isFlush() && $this->isRoyal()); } } echo "First Card: 2s 3h 4h 5h As\n\n"; $h = new Hand('2s 3h 4h 5h As'); $h->evalHand(); $h->getRank(); echo "\n\nSecond Card: 2s 3h 4h 5h 6s\n\n"; $h = new Hand('2s 3h 4h 5h 6s'); $h->evalHand(); $h->getRank(); echo "\n\nThird Card: 2h 3h 4h 5h 6h\n\n"; $h = new Hand('2h 3h 4h 5h 6h'); $h->evalHand(); $h->getRank(); echo "\n\nFourth Card: As Ks Qs Js 10s\n\n"; $h = new Hand('As Ks Qs Js 10s'); $h->evalHand(); $h->getRank();
Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
First Card: 2s 3h 4h 5h As Deprecated: Creation of dynamic property Hand::$suits is deprecated in /in/aE1M7 on line 65 Deprecated: Creation of dynamic property Hand::$fours is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$threes is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/aE1M7 on line 73 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/aE1M7 on line 75 Deprecated: Creation of dynamic property Hand::$straight is deprecated in /in/aE1M7 on line 200 Straight Second Card: 2s 3h 4h 5h 6s Deprecated: Creation of dynamic property Hand::$suits is deprecated in /in/aE1M7 on line 65 Deprecated: Creation of dynamic property Hand::$fours is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$threes is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/aE1M7 on line 73 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/aE1M7 on line 75 Deprecated: Creation of dynamic property Hand::$straight is deprecated in /in/aE1M7 on line 202 Straight Third Card: 2h 3h 4h 5h 6h Deprecated: Creation of dynamic property Hand::$suits is deprecated in /in/aE1M7 on line 65 Deprecated: Creation of dynamic property Hand::$fours is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$threes is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/aE1M7 on line 73 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/aE1M7 on line 75 Deprecated: Creation of dynamic property Hand::$straight is deprecated in /in/aE1M7 on line 202 Flush Fourth Card: As Ks Qs Js 10s Deprecated: Creation of dynamic property Hand::$suits is deprecated in /in/aE1M7 on line 65 Deprecated: Creation of dynamic property Hand::$fours is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$threes is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/aE1M7 on line 70 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/aE1M7 on line 73 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/aE1M7 on line 75 Deprecated: Creation of dynamic property Hand::$straight is deprecated in /in/aE1M7 on line 202 Flush
Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27
First Card: 2s 3h 4h 5h As Straight Second Card: 2s 3h 4h 5h 6s Straight Third Card: 2h 3h 4h 5h 6h Flush Fourth Card: As Ks Qs Js 10s Flush

preferences:
109.17 ms | 402 KiB | 157 Q