|
-
Mar 18th, 2004, 03:16 PM
#1
Thread Starter
Lively Member
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.
-
Mar 18th, 2004, 03:46 PM
#2
Hyperactive Member
Everything can be found on www.php.net....
But what you want is something like
PHP Code:
<?php
$a = 1 + 1 /* Add */
$b = 1 - 1 /* Substract */
$c = 1 * 1 /* Multiply */
$d = 1 / 1 /* Divide */
?>
Or did I get you wrong?
Grtz,
Bloged
-
Mar 18th, 2004, 03:54 PM
#3
Thread Starter
Lively Member
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>
-
Mar 18th, 2004, 03:58 PM
#4
Hyperactive Member
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
-
Mar 18th, 2004, 04:14 PM
#5
Thread Starter
Lively Member
Yes, that would work! But what about the code for the calculation?
-
Mar 18th, 2004, 04:40 PM
#6
Hyperactive Member
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
-
Mar 18th, 2004, 04:45 PM
#7
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.
-
Mar 18th, 2004, 09:44 PM
#8
Stuck in the 80s
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?
-
Mar 19th, 2004, 12:36 AM
#9
nah.. i just started making my lunch around that time. i do get distracted too easily though..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|