Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

my calculator is not working idk whats wrong

im sry if this question has been asked b4

this is my code:

index.php:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php
include_once 'includes/calc.inc.php';
 ?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form action="" method="post">
      <input type="text" name="num1" placeholder="Number 1...">
      <input type="text" name="num2" placeholder="Number 2...">
      <select name="oper">
        <option type="text" value="none">None</option>
        <option type="text" value="add">Add</option>
        <option type="text" value="sub">Subtract</option>
        <option type="text" value="mul">Multiply</option>
        <option type="text" value="div">Divide</option>
      </select>
      <button type="submit" name="submit">Submit</button>
    </form>
    <?php
    


    $number1 = $_POST['num1'];
    $number2 = $_POST['num2'];
    $operator = $_POST['oper'];

    $calc = new Calculator;
    $calc->Calc($number1, $number2, $operator);
     ?>

  </body>
</html>

calc.inc.php:

<?php

class Calculator {

    public $num1;
    public $num2;
    public $oper;
    public $result;

    public function Calc($num1in, $num2in, $operin) {
        $this->num1 = $num1in;
        $this->num2 = $num2in;
        $this->oper = $operin;

        switch ($this->oper) {
        case 'none':
            $this->result = "Please select method of calculating";
            break;

        case 'add':
            $this->result = $this->num1 + $this->num2;
            break;

        case 'sub':
            $this->result = $this->num1 - $this->num2;
            break;

        case 'mul':
            $this->result = $this->num1 * $this->num2;
            break;

        case 'div':
            $this->result = $this->num1 / $this->num2;
            break;

        default:
            echo "error";
            break;

            return $this->result;
        }
    }
}

i saw a calculator on github and compared and both code looked about the same. it might be the return statement at the end of the code in calc.inc.php but idk. i just finished learning php and this is my first project all help appreciated 🙂

>Solution :

You’re not displaying the result. After the line $calc->Calc($number1, $number2, $operator); you should add something like:`

print 'Result = '.$calc->return;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading