Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
<!DOCTYPE HTML> <html> <head> <title>PHP Highlighting</title> </head> <body> <?php highlight_file(__FILE__); ?> </body> </html>

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php // Базовая корзина покупок, содержащая список добавленных // продуктов и количество каждого продукта. Включает метод, // вычисляющий общую цену элементов корзины с помощью // callback-замыкания. class Cart { const PRICE_BUTTER = 1.20; const PRICE_MILK = 3.60; const PRICE_EGGS = 6.95; protected $products = array(); public function add($product, $quantity) { $this->products[$product] = $quantity; } public function getQuantity($product) { return isset($this->products[$product]) ? $this->products[$product] : FALSE; } public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk($this->products, $callback); return round($total, 2); } } $my_cart = new Cart; // Добавляем несколько элементов в корзину $my_cart->add('butter', 2); $my_cart->add('milk', 3); $my_cart->add('eggs', 6); // Выводим общую сумму с 5% налогом на продажу. print $my_cart->getTotal(0.05) . "\n"; // Результатом будет 54.29 ?>
Copy Clear