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();

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.3.40.0120.00319.03
8.3.30.0080.00819.02
8.3.20.0080.00021.73
8.3.10.0040.00421.76
8.3.00.0050.00321.71
8.2.170.0070.01422.96
8.2.160.0070.00720.60
8.2.150.0050.00324.18
8.2.140.0030.00624.66
8.2.130.0090.00019.95
8.2.120.0080.00026.35
8.2.110.0100.01322.13
8.2.100.0090.00317.97
8.2.90.0030.00618.09
8.2.80.0050.00318.29
8.2.70.0000.00918.18
8.2.60.0030.00618.47
8.2.50.0040.00418.10
8.2.40.0040.00419.61
8.2.30.0040.00420.50
8.2.20.0020.00518.09
8.2.10.0000.00819.41
8.2.00.0080.00018.27
8.1.270.0080.00023.99
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0030.00622.32
8.1.230.0120.00022.75
8.1.220.0040.00417.91
8.1.210.0080.00018.77
8.1.200.0070.00317.47
8.1.190.0030.00517.48
8.1.180.0060.00318.10
8.1.170.0080.00018.76
8.1.160.0030.00518.91
8.1.150.0030.00618.91
8.1.140.0080.00019.23
8.1.130.0040.00417.43
8.1.120.0000.00717.57
8.1.110.0040.00717.59
8.1.100.0090.00017.61
8.1.90.0030.00617.48
8.1.80.0040.00417.48
8.1.70.0000.00917.55
8.1.60.0080.00417.61
8.1.50.0030.00617.61
8.1.40.0040.00417.60
8.1.30.0030.00617.63
8.1.20.0000.00817.69
8.1.10.0000.00817.66
8.1.00.0030.00517.61
8.0.300.0050.00319.65
8.0.290.0030.00616.88
8.0.280.0000.00718.56
8.0.270.0040.00417.41
8.0.260.0000.00716.99
8.0.250.0030.00317.00
8.0.240.0070.00017.12
8.0.230.0070.00017.14
8.0.220.0000.00716.93
8.0.210.0000.00817.02
8.0.200.0030.00517.06
8.0.190.0000.00917.10
8.0.180.0040.00416.98
8.0.170.0050.00317.09
8.0.160.0030.00617.00
8.0.150.0000.00816.93
8.0.140.0030.00617.04
8.0.130.0000.00713.48
8.0.120.0060.00317.04
8.0.110.0030.00616.95
8.0.100.0030.00516.92
8.0.90.0040.00417.00
8.0.80.0070.01216.96
8.0.70.0040.00417.11
8.0.60.0060.00317.01
8.0.50.0050.00216.88
8.0.30.0150.00417.45
8.0.20.0180.00717.48
8.0.10.0040.00417.19
8.0.00.0120.00716.93
7.4.330.0000.00515.55
7.4.320.0050.00216.76
7.4.300.0040.00416.65
7.4.290.0080.00016.70
7.4.280.0030.00316.75
7.4.270.0030.00716.46
7.4.260.0040.00416.70
7.4.250.0000.00716.47
7.4.240.0070.00016.72
7.4.230.0000.00716.75
7.4.220.0050.00316.55
7.4.210.0080.00616.64
7.4.200.0030.00516.55
7.4.130.0060.02116.53
7.4.120.0120.00616.66
7.4.110.0060.01616.63
7.4.100.0110.01116.75
7.4.90.0090.00916.68
7.4.80.0110.00719.39
7.4.70.0110.01116.68
7.4.60.0030.01616.46
7.4.50.0040.01116.59
7.4.40.0130.00316.65
7.4.00.0090.00614.81
7.3.330.0000.00513.47
7.3.320.0030.00313.32
7.3.310.0040.00416.33
7.3.300.0070.00016.42
7.3.290.0090.00616.41
7.3.280.0110.01316.44
7.3.260.0140.00716.57
7.3.240.0100.00916.56
7.3.230.0090.00916.49
7.3.210.0130.00316.37
7.3.200.0060.01316.71
7.3.190.0170.00316.39
7.3.180.0030.01316.43
7.3.170.0060.01116.44
7.3.160.0090.01316.63
7.3.40.0060.00614.90
7.3.30.0090.01614.68
7.3.20.0060.00616.65
7.3.10.0070.01616.41
7.3.00.0080.00416.84
7.2.330.0170.00716.51
7.2.320.0060.01216.82
7.2.310.0070.01016.68
7.2.300.0070.01016.75
7.2.290.0070.01616.55
7.2.170.0140.00015.23
7.2.160.0000.01415.12
7.2.150.0090.00316.68
7.2.140.0070.00716.71
7.2.130.0040.00817.09
7.2.120.0130.00816.99
7.2.110.0130.00316.88
7.2.100.0030.00916.83
7.2.90.0060.00617.05
7.2.80.0080.00416.71
7.2.70.0150.00616.99
7.2.60.0190.00616.80
7.2.50.0220.00016.85
7.2.40.0110.01116.75
7.2.30.0110.00416.71
7.2.20.0090.00316.94
7.2.10.0060.01717.00
7.2.00.0000.01516.92
7.1.280.0040.01115.98
7.1.270.0030.00915.84
7.1.260.0030.00815.67
7.1.250.0120.00915.89

preferences:
31 ms | 400 KiB | 5 Q