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; 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(){ var_dump($this->getHighCard()); var_dump($this->isOnePair()); var_dump($this->isTwoPair()); var_dump($this->getThrees()); var_dump($this->getFours()); var_dump($this->isFullHouse()); var_dump($this->isFlush()); var_dump($this->isStraight()); var_dump($this->isStraightFlush()); var_dump($this->isRoyalFlush()); } 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() { return (count($this->pairs) == 1 ? true : false); } public function isTwoPair() { return (count($this->pairs) == 2 ? true : false); } public function getThrees() { return $this->threes; } public function getFours() { return $this->fours; } public function hasAce() { return isset($this->hands['14']); } public function isFullHouse() { 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) { ($count == 5 ? $this->flush = true : $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; } else if ($this->isStraightRegular()) { $this->straight = true; } else { $this->straight = false; } return $this->straight; } public function isStraightFlush() { 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(){ 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(); echo "\n\nSecond Card: 2s 3h 4h 5h 6s\n\n"; $h = new Hand('2s 3h 4h 5h 6s'); $h->evalHand(); echo "\n\nThird Card: 2h 3h 4h 5h 6h\n\n"; $h = new Hand('2h 3h 4h 5h 6h'); $h->evalHand(); echo "\n\nFourth Card: As Ks Qs Js 10s\n\n"; $h = new Hand('As Ks Qs Js 10s'); $h->evalHand();

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.1.110.0070.00417.53
8.1.100.0040.00417.57
8.1.90.0030.00517.66
8.1.80.0080.00417.57
8.1.70.0070.00017.61
8.1.60.0060.00317.70
8.1.50.0030.00517.68
8.1.40.0060.00317.61
8.1.30.0080.00017.75
8.1.20.0040.00717.77
8.1.10.0040.00417.75
8.1.00.0000.00817.66
8.0.240.0030.00317.17
8.0.230.0040.00417.00
8.0.220.0000.00717.10
8.0.210.0040.00417.04
8.0.200.0070.00017.13
8.0.190.0040.00417.06
8.0.180.0030.00517.09
8.0.170.0040.00417.15
8.0.160.0050.00317.07
8.0.150.0000.00716.90
8.0.140.0000.00816.97
8.0.130.0000.00713.46
8.0.120.0000.00717.03
8.0.110.0070.00017.07
8.0.100.0000.00816.93
8.0.90.0040.00416.97
8.0.80.0080.01116.97
8.0.70.0030.00517.01
8.0.60.0050.00316.97
8.0.50.0030.00517.00
8.0.30.0060.01317.17
8.0.20.0270.03417.06
8.0.10.0030.00517.08
8.0.00.0120.00716.96
7.4.300.0000.00616.64
7.4.290.0040.00416.77
7.4.280.0040.00416.62
7.4.270.0030.00716.63
7.4.260.0000.00816.70
7.4.250.0050.00316.60
7.4.240.0040.00416.73
7.4.230.0040.00316.63
7.4.220.0030.00516.68
7.4.210.0100.00616.70
7.4.200.0050.00316.73
7.4.140.0110.00817.86
7.4.130.0030.01416.60
7.4.120.0090.00916.61
7.4.110.0140.00316.61
7.4.100.0170.00316.60
7.4.90.0140.01016.54
7.4.80.0220.00319.39
7.4.70.0130.01316.66
7.4.60.0100.00716.64
7.4.50.0120.00416.44
7.4.40.0140.00316.52
7.4.10.0060.01616.81
7.4.00.0080.00815.92
7.3.330.0000.00613.48
7.3.320.0070.00013.43
7.3.310.0080.00016.52
7.3.300.0030.00316.44
7.3.290.0100.00416.55
7.3.280.0090.00816.45
7.3.260.0120.01216.64
7.3.240.0100.00816.70
7.3.230.0060.01216.32
7.3.210.0150.01116.50
7.3.200.0090.00916.79
7.3.190.0140.00516.74
7.3.180.0180.00016.45
7.3.170.0100.00716.52
7.3.160.0100.00716.67
7.3.130.0120.00916.41
7.3.120.0060.01216.50
7.3.110.0070.01016.31
7.3.100.0160.00016.47
7.3.90.0090.00616.38
7.3.80.0100.00616.47
7.3.70.0110.00716.49
7.3.60.0030.01016.50
7.3.50.0030.01016.39
7.3.40.0110.00315.73
7.3.30.0040.00915.69
7.3.20.0060.01116.47
7.3.10.0120.00316.53
7.3.00.0130.00516.46
7.2.330.0060.01216.53
7.2.320.0090.01216.97
7.2.310.0070.01016.83
7.2.300.0130.00416.98
7.2.290.0070.01416.61
7.2.260.0040.01416.72
7.2.250.0030.01916.63
7.2.240.0070.00416.71
7.2.230.0030.01316.45
7.2.220.0070.00716.74
7.2.210.0070.00716.78
7.2.200.0030.01016.85
7.2.190.0100.00316.76
7.2.180.0100.00616.81
7.2.170.0060.01015.81
7.2.160.0030.01115.88
7.2.150.0090.00816.87
7.2.140.0120.00516.94
7.2.130.0090.00916.71
7.2.120.0060.01416.94
7.2.110.0320.01016.80
7.2.100.0050.01316.73
7.2.90.0130.01016.95
7.2.80.0110.01016.65
7.2.70.0130.00816.76
7.2.60.0100.01116.76
7.2.50.0100.00816.75
7.2.40.0070.00916.88
7.2.30.0080.00916.79
7.2.20.0080.01017.01
7.2.10.0110.00616.86
7.2.00.0100.00716.77
7.1.330.0100.00717.28
7.1.320.0070.00717.34
7.1.310.0030.01017.54
7.1.300.0130.00017.41
7.1.290.0060.00917.50
7.1.280.0110.00715.84
7.1.270.0070.00715.78
7.1.260.0720.00915.72
7.1.250.0090.01215.91
7.1.240.0130.00717.30
7.1.230.0070.00717.27
7.1.220.0000.01317.26
7.1.210.0100.00317.18
7.1.200.0080.00817.37
7.1.190.0100.00717.44
7.1.180.0000.01817.36
7.1.170.0040.00717.31
7.1.160.0120.00617.26
7.1.150.0000.01217.52
7.1.140.0040.01217.35
7.1.130.0070.00517.26
7.1.120.0000.01517.38
7.1.110.0070.00717.31
7.1.100.0030.00617.39
7.1.90.0070.00717.39
7.1.80.0040.01117.39
7.1.70.0000.01117.18
7.1.60.0070.00717.38
7.1.50.0150.00017.30
7.1.40.0030.01317.43
7.1.30.0090.00617.32
7.1.20.0060.01317.27
7.1.10.0070.01317.41
7.1.00.0000.01017.35

preferences:
49.65 ms | 406 KiB | 5 Q