]> git repositories - basic-calc.git/commitdiff
first commit
authorbochard <mail@tenkyuu.dev>
Tue, 29 Apr 2025 12:21:15 +0000 (20:21 +0800)
committerbochard <mail@tenkyuu.dev>
Tue, 29 Apr 2025 12:21:15 +0000 (20:21 +0800)
README.md [new file with mode: 0644]
backend.php [new file with mode: 0644]
index.php [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..d2e1de0
--- /dev/null
+++ b/README.md
@@ -0,0 +1,4 @@
+### basic calc
+
+simple calculator on the web, based from php.
+practice project for php.
\ No newline at end of file
diff --git a/backend.php b/backend.php
new file mode 100644 (file)
index 0000000..8566bb3
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+  if(isset($_GET["calculate"])){
+    $input1 = (float) $_GET["num1"];
+    $input2 = (float) $_GET["num2"];
+    $operator = $_GET["operation"];
+    $result = null;
+
+    if(empty($input1) || empty($input2)){
+      echo "specify a number into the first and second input.";
+      return;
+    }
+    elseif($operator == "select"){
+      echo "select an operation.";
+      return;
+    }
+
+    switch($operator){
+      case "add":
+        $result = $input1 + $input2;
+        echo "The sum is {$result}";
+        break;
+      case "subtract":
+        $result = $input1 - $input2;
+        echo "The difference is {$result}";
+        break;
+      case "multiply":
+        $result = $input1 * $input2;
+        echo "The product is {$result}";
+        break;
+      case "division":
+        $result = $input1 / $input2;
+        echo "The quotient is {$result}";
+        break;
+    }
+  }
+?>
\ No newline at end of file
diff --git a/index.php b/index.php
new file mode 100644 (file)
index 0000000..ca43108
--- /dev/null
+++ b/index.php
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>basic calc</title>
+</head>
+<body>
+  <h1>basic calc</h1>
+  <form action="<?php htmlspecialchars($_SERVER["PHP_SELF"])?>" method="GET">
+    <label for="num1">first number:</label>
+      <input type="text" name="num1" id="num1"><br>
+    <label for="num1">second number:</label>
+      <input type="text" name="num2" id="num2"><br>
+    <label for="operation">operation:</label>
+      <select name="operation" id="operation">
+        <option value="select" selected>Select</option>
+        <option value="add">Add</option>
+        <option value="subtract">Subtract</option>
+        <option value="multiply">Multiply</option>
+        <option value="divide">Divide</option>
+      </select><br>
+    <input type="submit" name="calculate" value="calculate">
+  </form>
+  <?php include("./backend.php");?>
+</body>
+</html>
\ No newline at end of file