Skip to content

Instantly share code, notes, and snippets.

@katsube
Created October 28, 2023 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsube/28c4cf4d0fbcfd7729bd0a4bb8140410 to your computer and use it in GitHub Desktop.
Save katsube/28c4cf4d0fbcfd7729bd0a4bb8140410 to your computer and use it in GitHub Desktop.
<?php
/**
* 武器屋経営
* - 在庫数と在庫金額を調べる
*/
//------------------------------------
// 在庫の一覧
//------------------------------------
$weapons = [
['id'=>1, 'name'=>'桧の棒', 'price'=>100, 'stock'=>10],
['id'=>2, 'name'=>'鋼の剣', 'price'=>1000, 'stock'=>3],
['id'=>3, 'name'=>'炎の剣', 'price'=>2000, 'stock'=>1]
];
//------------------------------------
// 在庫を調べる
//------------------------------------
// 合計数
$totalStock = totalStock(); // 関数から在庫数(整数)を取得
printf("合計在庫数: %d個\n", $totalStock);
// 合計金額
$totalPrice = totalPrice(); // 関数から金額(整数)を取得
printf("合計金額: %d円\n", $totalPrice);
/**
* 合計金額を返却する
*/
function totalPrice() {
global $weapons; // グローバル変数を関数内で使う
$total = 0; // 合計金額
// ★ここを回答する★
// このコメントは削除してください
}
/**
* 合計在庫数を返却する
*/
function totalStock() {
global $weapons; // グローバル変数を関数内で使う
$total = 0; // 合計在庫数
// 合計在庫数を計算
for($i=0; $i<count($weapons); $i++) {
$total += $weapons[$i]['stock'];
}
return($total); // 合計在庫数を返却
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment