3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Deck{ public $cards = array(); //creates an instance of a deck of cards (works) public function __construct(){ $values =array('2','3','4','5','6','7','8','9','10','J','Q','K','A'); $suits =array('Diamond','Club','Heart','Spade'); foreach ($suits as $suit) { foreach($values as $value){ $this->cards[] = "$value of $suit's"; } } } public function numberOfDecks($number){ $cards = $this->cards; $this->number = $number; for($i = 0 ; $i < $number; $i++){ $this->cards = array_merge($this->cards, $cards); } } } $deck = new Deck();//works as expected // how many cards are intially constructed? echo "count of cards in new deck: " . count($deck->cards) . "\n<br/>\n"; // add 3 more decks of cards $deck->numberOfDecks(3);//trouble echo "count of cards after adding 3 decks: " . count($deck->cards); // $shuffled = shuffle($deck->cards);//works as expected // var_dump($deck);
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
count of cards in new deck: 52 <br/> Deprecated: Creation of dynamic property Deck::$number is deprecated in /in/h3Sv5 on line 23 count of cards after adding 3 decks: 208
Output for 5.6.0 - 5.6.38, 7.0.0 - 7.0.31, 7.1.0 - 7.1.33, 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.33
count of cards in new deck: 52 <br/> count of cards after adding 3 decks: 208

preferences:
144.38 ms | 409 KiB | 5 Q