2
2

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 3 years have passed since last update.

phpで簡易電卓を作る①

Last updated at Posted at 2020-03-11

まずは基本的な機能のみ

2つの数値と四則演算を入力すると計算した値が表示される。

index.php
<!DOCTYPE html>
<html lang='ja'>
  <head>
    <meta charset="utf-8">
    <title>Caluculator</title>
  </head>
  <body>
    <h1>電卓(簡易版)</h1>
    <form class="formula" action="calculate.php" method="post">
      数字1:<input type="string" name="number1"><br>
      記号:<select name="symbol">
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">×</option>
        <option value="/">÷</option>
      </select><br>
      数字2:<input type="string" name="number2" value=""><br>
      <input type="submit">
    </form>
    <h2>答え:<?php echo $_GET['answer']?></h2>
  </body>
</html>
calculate.php
<?php
$number1 = $_POST['number1'];
$symbol = $_POST['symbol'];
$number2 = $_POST['number2'];
$answer = '';
switch ($symbol) {
     case '+':
         $answer = $number1 + $number2;
         break;
     case '-':
         $answer = $number1 - $number2;
         break;
     case '*':
         $answer = $number1 * $number2;
         break;
     case '/':
         $answer = $number1 / $number2;
         break;
};
header('Location: index.php/?answer='.$answer);

課題

・数値入力欄に数値以外を入力されたらどうする?
・続けて計算ができない

上記の点を次回は直します。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?