0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで簡単な自動販売機作ってみた!!!

Posted at

まずは投入金額と飲みたいドリンクを定義する!

vending_machine.php
$money = 150;
$drink = '缶コーヒー';

swich文を使ってドリンクの値段を定義する!

vending_machine.php
switch ($drink){
    
    // $valueは商品の値段
    case '缶コーヒー':
        $value = 100; 
        break;
        
    case '緑茶':
        $value = 150;
        break;
        
    case 'コーラ':
        $value = 130;
        break;
    
    case 'ナタデココ':
        $value = 120;
        break;
}

自動販売機の関数の定義とその関数の呼び出し式を作る!

vending_machine.php

//購入する際の関数式
function buy ($money, $value, $drink){
    if($money == $value){
        echo $drink.'をお買い上げありがとうございます';
    } elseif ($money > $value){
        $sum = $money - $value;
        echo $sum.'円のお釣りです';
    } else {
        echo 'お金が足りません!';
    }
}

//関数の呼び出し式
buy ($money, $value, $drink);
vending_machine.php
$ php vending_machine.php
50円のお釣りです

これで自動販売機の関数の完成!!!
あとは投入金額を変えると表示が変わっていきます!!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?