calculator with conditions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

calculator with conditions

Post by myplaces »

I need to substract from number A number B only if is clicked option(form) near number B. Then result multiple with number C.
But if option (form) near B is not clicked, then multiply only A with C.
Can someone write some tips?

to substract, i use this.....
$calc = ($_POST['numberA']-$_POST['NumberB']);

i need to use if? i have very poor php knowledges

may be any link where i can read?
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: calculator with conditions

Post by Hannes2k »

Hi,
just read a general php tutorial to improve your php knownledge. Then the answer would be clear. (You have to use if to realize what you want).
PHPyrox
Forum Newbie
Posts: 4
Joined: Sat Nov 29, 2008 12:45 pm

Re: calculator with conditions

Post by PHPyrox »

I guess something like this:

<?php
//First to make the variables easier to use:
$a = $_POST['numberA'];
$c = $_POST['numberC'];

//Checks if B is set, if else, sets b=0 since a-0 wont change anything
//BTW, isset($variableName) checks if the variable exists, very useful function...
if(isset($_POST['numberB']))
{
$b = $_POST['numberB'];
}
else
{ $b = 0; }

//Returns the result
$result = ($a - $b) * $c;
echo $result;
?>

Might aswell be something wrong with the <form method="POST"> maybe...

Good luck!
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

Re: calculator with conditions

Post by myplaces »

Thanks
Post Reply