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 git.master, git.master_jit, rfc.property-hooks
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) } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
17.6 ms | 403 KiB | 8 Q