3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Cart { protected array $cart = []; protected array $fees = []; public function __construct() { // generate test Cart $testProducts = [ ['id' => 1 , 'nb' => 22, 'price' => 23], ['id' => 383 , 'nb' => 38, 'price' => 19], ['id' => 412, 'nb' => 11, 'price' => 34], ]; foreach ($testProducts as $testProduct) { while ($testProduct['nb']-- > 0) { $this->addProduct($testProduct['id'], $testProduct['price']); } } } public function cart_contents(): array { return $this->cart; } public function addProduct(int $id, int $price) { $this->cart[] = ['product_id' => $id, 'price' => $price]; } public function add_fee(string $txt, float $price) { $this->fees[] = ['txt' => $txt, 'price' => $price]; } function apply_discount_for_quantity() { // For sample, I let you choose of to define the rules and send it here $discount_rules = [ ['eligible_product_ids' => [383, 411], 'required_count' => 12, 'discount_percent' => 4], ['eligible_product_ids' => [412], 'required_count' => 10, 'discount_percent' => 5], ]; // Will use counter per lligible product $discounts_counter_cache = []; // cycle through the products in the cart to count thecantidad de productos elegibles foreach ($this->cart_contents() as $product) { foreach ($discount_rules as $rule) { // if product is eligible to one of our discount rule if (!in_array($product['product_id'], $rule['eligible_product_ids'])) { // product not eligible for a discount continue; } // let's count of many product whe got $discounts_counter_cache[ $product['product_id'] ] = ($discounts_counter_cache[ $product['product_id'] ] ?? 0) + 1; // Apply per elligible product only when it's reach the limit if ($discounts_counter_cache[ $product['product_id'] ] == $rule['required_count']) { echo sprintf(' -- Found discount of %d%% to apply on the batch of %d product (id %d) in the cart' . PHP_EOL, $rule['discount_percent'], $rule['required_count'], $product['product_id'] ); // We trigger the discount $this->add_fee( 'Descuento por cantidad: ' . $rule['discount_percent'] .'% por '. $rule['required_count'] .'x'. $product['product_id'], // You can also set the value in the rule -($product['price'] * $rule['required_count'] * ($rule['discount_percent'] / 100)) ); // Reset counter $discounts_counter_cache[ $product['product_id'] ] = 0; } } } } public function show_cart() { $products = []; foreach ($this->cart as $entry) { if (!isset($products[$entry['product_id']])) { $products[$entry['product_id']] = [ 'id' => $entry['product_id'], 'nb' => 0, 'price' => $entry['price'], 'total' => $entry['price'], ]; } $products[$entry['product_id']]['nb']++; $products[$entry['product_id']]['price'] += $entry['price']; } echo 'Cart:'. PHP_EOL; foreach ($products as $product) { echo sprintf( '> Product Id:"%d" - %d$ x %d = %d$'.PHP_EOL, $product['id'], $product['price'], $product['nb'], $product['total'] ); } echo PHP_EOL; if (!empty($this->fees)) { echo 'Fees:'.PHP_EOL; $totalFees = ['nb' => 0, 'total' => 0]; foreach ($this->fees as $fee) { $totalFees['nb']++; $totalFees['total']+= $fee['price']; echo sprintf( '> %d$ - %s' . PHP_EOL, $fee['price'], $fee['txt'] ); } echo sprintf( '>> Total of %d discounts for %d$' . PHP_EOL, $totalFees['nb'], $totalFees['total'] ); } } } $cart = new Cart(); echo 'Before discount:'.PHP_EOL; echo $cart->show_cart(); $cart->apply_discount_for_quantity(); echo PHP_EOL.'After discount:'.PHP_EOL; echo $cart->show_cart();

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.5.10.0050.00321.94
8.5.00.0130.00920.02
8.4.160.0110.01223.96
8.4.150.0020.00014.05
8.4.140.0110.01117.55
8.4.130.0130.00817.72
8.4.120.0050.00420.49
8.4.110.0050.00522.61
8.4.100.0120.00917.71
8.4.90.0120.00920.59
8.4.80.0110.00817.73
8.4.70.0100.01117.84
8.4.60.0040.00418.90
8.4.50.0100.01217.89
8.4.40.0130.00519.34
8.4.30.0100.01018.92
8.4.20.0160.00617.77
8.4.10.0060.00324.14
8.3.280.0140.00920.19
8.3.270.0110.00816.52
8.3.260.0090.00816.59
8.3.250.0100.01018.96
8.3.240.0100.00916.81
8.3.230.0110.00916.76
8.3.220.0140.00617.25
8.3.210.0080.01218.46
8.3.200.0050.00416.44
8.3.190.0080.00817.34
8.3.180.0090.00816.76
8.3.170.0030.00618.91
8.3.160.0100.01016.62
8.3.150.0100.00718.92
8.3.140.0110.00817.37
8.3.130.0040.00418.38
8.3.120.0130.00618.96
8.3.110.0000.01020.94
8.3.100.0130.00617.03
8.3.90.0040.00426.77
8.3.80.0060.00319.36
8.3.70.0060.01616.75
8.3.60.0150.00018.56
8.3.50.0110.00718.35
8.3.40.0070.00719.94
8.3.30.0140.00418.48
8.3.20.0050.00324.18
8.3.10.0060.00324.66
8.3.00.0050.00326.16
8.2.290.0110.00820.65
8.2.280.0040.00518.75
8.2.270.0070.01117.26
8.2.260.0060.00318.91
8.2.250.0080.00018.39
8.2.240.0120.00617.33
8.2.230.0060.00322.58
8.2.220.0060.00324.06
8.2.210.0040.00426.77
8.2.200.0080.00418.88
8.2.190.0160.00316.63
8.2.180.0120.00916.75
8.2.170.0060.01018.95
8.2.160.0110.00322.96
8.2.150.0080.00025.66
8.2.140.0050.00324.66
8.2.130.0040.00426.16
8.2.120.0030.00620.80
8.2.110.0030.00619.17
8.2.100.0040.00818.05
8.2.90.0050.00317.88
8.2.80.0040.00418.73
8.2.70.0050.00317.75
8.2.60.0030.00517.63
8.2.50.0190.00019.49
8.2.40.0140.00519.49
8.2.30.0100.00719.49
8.2.20.0120.00619.49
8.2.10.0070.00719.49
8.2.00.0100.00619.49
8.1.330.0090.01022.22
8.1.320.0100.01016.06
8.1.310.0070.00716.89
8.1.300.0100.00018.41
8.1.290.0060.00330.84
8.1.280.0090.00925.92
8.1.270.0080.00024.66
8.1.260.0050.00326.35
8.1.250.0000.00728.09
8.1.240.0060.00322.74
8.1.230.0090.00317.79
8.1.220.0000.00817.79
8.1.210.0050.00318.77
8.1.200.0040.00417.61
8.1.190.0050.00317.25
8.1.180.0180.00019.49
8.1.170.0080.00919.49
8.1.160.0070.00719.49
8.1.150.0120.00619.49
8.1.140.0090.00919.49
8.1.130.0070.00719.49
8.1.120.0160.00019.49
8.1.110.0120.00519.49
8.1.100.0050.01019.49
8.1.90.0100.00719.49
8.1.80.0120.00419.49
8.1.70.0070.01119.49
8.1.60.0140.00019.49
8.1.50.0120.00419.49
8.1.40.0060.00919.49
8.1.30.0130.00519.49
8.1.20.0130.00419.49
8.1.10.0080.00819.49
8.1.00.0110.00819.49
8.0.300.0040.00418.77
8.0.290.0000.00816.88
8.0.280.0120.00319.49
8.0.270.0060.00619.49
8.0.260.0060.00619.49
8.0.250.0000.01219.49
8.0.240.0080.00819.49
8.0.230.0130.00019.49
8.0.220.0070.00519.49
8.0.210.0110.00319.49
8.0.200.0080.00819.49
8.0.190.0140.00219.49
8.0.180.0100.00519.49
8.0.170.0110.00519.49
8.0.160.0150.00019.49
8.0.150.0080.00619.49
8.0.140.0130.00319.49
8.0.130.0150.00019.49
8.0.120.0070.00519.49
8.0.110.0080.00419.49
8.0.100.0080.00819.49
8.0.90.0090.00519.49
8.0.80.0040.00919.49
8.0.70.0100.00519.49
8.0.60.0060.00619.49
8.0.50.0070.00719.49
8.0.30.0110.00319.49
8.0.20.0080.00519.49
8.0.10.0170.00019.49
5.6.300.0150.00816.18

preferences:
35.55 ms | 403 KiB | 5 Q