Results 1 to 9 of 9

Thread: PHP-calculator

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    70

    Question PHP-calculator

    Hi! I'm relly new to php and would appreciate if someone could help me on this. I want to make a simple calculator with these functions:

    - Add
    - Multiply
    - Divide

    Thanks for your help!
    Last edited by Mutuz; Mar 18th, 2004 at 03:37 PM.

  2. #2
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    Everything can be found on www.php.net....

    But what you want is something like

    PHP Code:
    <?php
      $a 
    /* Add */
      
    $b /* Substract */
      
    $c /* Multiply */
      
    $d /* Divide */
    ?>
    Or did I get you wrong?

    Grtz,

    Bloged

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    70
    Thanks for your answer! Well, I want to make the calculator with three different inputs, but I'm not really sure if that's the best/correct way to do it.. I'm using the first for the first number, the second to choose a function, and the third for the second number:

    <form method="post" action="calc.php">
    <input type="text" name="input1" size="8">
    <br>
    <input type="text" name="tegn" size="8">
    <br>
    <input type="text" name="input2" size="8">
    <input type="submit"><br>
    </form>

  4. #4
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    You could make an SELECT box for the 'functions' you support:

    Something like:
    PHP Code:
    <SELECT name="function">
      <
    OPTION value="add">add</OPTION>
      <
    OPTION value="sub">substract</OPTION>
      <
    OPTION value="mul">multiply</OPTION>
      <
    OPTION value="div">divide</OPTION>
    </
    SELECT
    Grtz,

    Bloged

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    70

    Smile

    Yes, that would work! But what about the code for the calculation?

  6. #6
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    With functions:
    PHP Code:
    <?php
     
    function add($i$j)
     {
      return 
    $i $j;
     }

      function 
    substract($i$j)
     {
      return 
    $i $j;
     }

      function 
    multiply($i$j)
     {
      return 
    $i $j;
     }
      
     function 
    divide($i$j)
     {
       return 
    $i $j;
     }

     if(
    $function == 'add')
     {
       
    $answer add($input1$input2);
     }
     elseif(
    $function == 'sub')
     {
       
    $answer substract($input1$input2);
     }
     elseif(
    $function == 'mul')
     {
       
    $answer multiply($input1$input2);
     }
     elseif(
    $function == 'add')
     {
       
    $answer divide($input1$input2);
     }
    ?>
    Without functions:
    PHP Code:
    <?php
     
    if($function == 'add')
     {
       
    $answer $input1 $input2;
     }
     elseif(
    $function == 'sub')
     {
       
    $answer $input1 $input2;
     }
     elseif(
    $function == 'mul')
     {
       
    $answer $input1 $input2;
     }
     elseif(
    $function == 'add')
     {
       
    $answer $input1 $input2;
     }
    ?>
    The one with functions is better if you want to calculate complexer stuff later!

    Grtz,

    Bloged

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    build a basic HTML form that has the two input boxes for the integers you want to add together, one named 'int1', the other 'int2'.. either make four radio buttons (i'm going to assume you want to subtract as well) or make four different command buttons, one for each math operation. for this, i'm going to take the idea of radio buttons, named 'rad1'. have the form POST to 'calc.php', and add in the code below:

    PHP Code:
    <?
      //set the default values for the error array
      //simple calculator
      if(!isset($_POST['int1'], $_POST['int2'], $_POST['rad1'])){
        //the user hasn't came here from a POST form, kill the script
        die("Error gathering form information!\n");
      }else{
        //grab the form elements and assign them variable names in an array
        $int[0] = $_POST['int1'];
        $int[1] = $_POST['int2'];
        $op = $_POST['rad1'];

        //check if the user entered two numeric values
        if(is_numeric($int[0]) && is_numeric($int[1])){
          //use switch() on $op to operate on the integers
          switch($op){
            case "+": //add the two integers together
              $out = $int[0] + $int[1];
              break;
            case "-": //subtract
              $out = $int[0] - $int[1];
              break;
            case "/": //divide
              $out = $int[0] / $int[1];
              break;
            case "*": //multiply
              $out = $int[0] * $int[1];
              break;
            default: //unrecognized operator, give error
              $out = "Inrecognizable math operator";
          }
          //prepare $out for output, checking if there was an error or not
          if(is_numeric($out)){
            //echo the original equation and the sum
            echo $int[0] . " " . $op . " " . $int[1] . " = " . $out . "\n";
          }else{
            //there was an error, kill the script
            die($out);
          }
        }else{
          //kill the script because of invalid integers
          die("You must enter numeric values!\n");
        }
      }
    ?>
    here's a demo of it with a working HTML form:
    http://david.gamersepitome.net/files/php/calc/calc.htm

    more on math operators:
    http://ca2.php.net/manual/en/languag...arithmetic.php

    edit: hmm, when i started replying no one had replied.. ah well.
    Like Archer? Check out some Sterling Archer quotes.

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by kows
    edit: hmm, when i started replying no one had replied.. ah well.
    You type too slow!

    How about making a derivative calculator. Who wants to try that?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    nah.. i just started making my lunch around that time. i do get distracted too easily though..
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width