Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php declare(strict_types=1);
// 消費税10%
const TAX = 0.1;
// 商品単体の値段を計算する
function calculate(int $price, int $count): float
{
return $price*$count*(1+TAX);
}
// レシートの文字列を返す
function showReceipt(array $items): string
{
$total_price = 0;
foreach ($items as $item) {
$total_price += calculate($item["price"], $item["count"]);
}
$item_name_list = array_column($items, "name");
return "レシート\n".implode("\n", $item_name_list)."\n¥$total_price\n\n";
}
$items_a = [
[
"name" => "消しゴム",
"count" => 2,
"price" => 60,
]
];
$items_b = [
[
"name" => "鉛筆",
"count" => 3,
"price" => 60,
],
[
"name" => "ペン",
"count" => 2,
"price" => 100,
],
];
echo showReceipt($items_a);
echo showReceipt($items_b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment