3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Cart{ private $cart_items = array(); private $number_of_cart_items; public function add_to_cart($item, $quantity = 1){ if(!isset($item) || !isset($item->ItemID)){ throw new Exception("Error adding item to cart"); } if(!array_key_exists($item->ItemID,$this->cart_items)){ $this->cart_items[$item->ItemID] = array("Item"=>$item,"Quantity"=>$quantity); }else{ $this->cart_items[$item->ItemID]["Quantity"] += $quantity; } $this->number_of_cart_items+=$quantity; return $this->cart_items; } } $itemArray = array( "ItemID" => 11, "ItemName" => "Kids check T-Shirt", "ShortDescription" => "A kids check T-Shirt", "LongDescription"=>"A kids check T-shirt perfect for formal occasions!", "ItemPrice" => 33.59, "ImagePath" => "kozzi-26129586-1591x2387.jpg", "QuantityAvailable" => 100, "ItemSupplier_SupplierID" => 1 ); $quantity = 1; $item = (object)$itemArray ; $cart = new Cart(); $add_to_cart = $cart->add_to_cart($item,$quantity); print_r($add_to_cart); var_dump($add_to_cart);
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Array ( [11] => Array ( [Item] => stdClass Object ( [ItemID] => 11 [ItemName] => Kids check T-Shirt [ShortDescription] => A kids check T-Shirt [LongDescription] => A kids check T-shirt perfect for formal occasions! [ItemPrice] => 33.59 [ImagePath] => kozzi-26129586-1591x2387.jpg [QuantityAvailable] => 100 [ItemSupplier_SupplierID] => 1 ) [Quantity] => 1 ) ) array(1) { [11]=> array(2) { ["Item"]=> object(stdClass)#1 (8) { ["ItemID"]=> int(11) ["ItemName"]=> string(18) "Kids check T-Shirt" ["ShortDescription"]=> string(20) "A kids check T-Shirt" ["LongDescription"]=> string(50) "A kids check T-shirt perfect for formal occasions!" ["ItemPrice"]=> float(33.59) ["ImagePath"]=> string(28) "kozzi-26129586-1591x2387.jpg" ["QuantityAvailable"]=> int(100) ["ItemSupplier_SupplierID"]=> int(1) } ["Quantity"]=> int(1) } }

preferences:
312.61 ms | 406 KiB | 330 Q