teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正

2019/04/09 01:52

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -1,6 +1,7 @@
1
1
  やり方は幾らでもあります。
2
2
  お好きなのをどうぞ。
3
3
 
4
+ **※おそらく何かの問題集や課題のようなので自身が理解できるものから採用してください**
4
5
 
5
6
  ```php
6
7
  //他で使わないなら変数にも入れなくてOK

1

修正

2019/04/09 01:52

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -34,5 +34,35 @@
34
34
  $sum = $price +($price * $taxRate);
35
35
  echo str_replace("〇〇",$sum,"税込価格は〇〇円です");
36
36
  ```
37
+ ```php
38
+ //消費税決まってるので他にも価格がきたときのために関数化とか
39
+ function taxCalc(int $price = 0):int
40
+ {
41
+ $taxRate = 0.08;
42
+ return $price +($price * $taxRate);
43
+ }
44
+ $price = 1000;
45
+ echo "税込価格は".taxCalc($price)."円です";
46
+ ```
47
+ ```
48
+ //商品扱うという観点からクラスにするとか
49
+ class Item
50
+ {
51
+ private $taxRate = 0.08;
52
+ public $price = 0;
53
+ public $name = '';
54
+ function __construct(string $name='',int $price = 0)
55
+ {
56
+ $this->name = $name;
57
+ $this->price = $price;
58
+ }
59
+ function taxPrice():int
60
+ {
61
+ return $this->price +($this->price * $this->taxRate);
62
+ }
63
+ }
64
+ $item = new Item('りんご',1000);
65
+ echo "税込価格は".$item->taxPrice()."円です";
66
+ ```
37
67
 
38
68
  etc.