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!:)
Printable View
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!:)
Everything can be found on www.php.net....
But what you want is something like
Or did I get you wrong?PHP Code:<?php
$a = 1 + 1 /* Add */
$b = 1 - 1 /* Substract */
$c = 1 * 1 /* Multiply */
$d = 1 / 1 /* Divide */
?>
Grtz,
Bloged
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..:blush: 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>
You could make an SELECT box for the 'functions' you support:
Something like:
Grtz,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>
Bloged
Yes, that would work!:) But what about the code for the calculation?
With functions:
Without 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);
}
?>
The one with functions is better if you want to calculate complexer stuff later!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;
}
?>
Grtz,
Bloged
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:
here's a demo of it with a working HTML form: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");
}
}
?>
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.
You type too slow!Quote:
Originally posted by kows
edit: hmm, when i started replying no one had replied.. ah well.
How about making a derivative calculator. Who wants to try that?
nah.. i just started making my lunch around that time. i do get distracted too easily though..