Build a calculator in PHP

I trying to build a calculator which will use a certain formula which will echo out the answer. I have an HTML form which gets the inputs and goes to the calc.php page and echos out the answer. I have this code but still cannot get it working. My html code [code]

Calculator
    <?php echo form_close();?>

</form>
[/code]

And this is my PHP code. [php]function calculate($num1, $num2, $num3)
{
$data = array() ;

$data[‘c’] = $num1 / 50;
$data[‘b’] = $num2 / 12;
$data[‘s’] = $num3 / 5;
$data[‘p’] = $data[‘c’] + $data[‘b’] - $data[‘s’];

if($data['s'] > 4)
{
    $data['s']= 4;
}
return $data ;

}

if (isset($_REQUEST[‘value’])) //Additional checks should be done tho.
{
$num1 = $_REQUEST[“num1”];
$num2 = $_REQUEST[“num2”];
$num3 = $_REQUEST[“num3”];

$data = calculate($num1, $num2, $num3) ;
echo "{$data['c']} + {$data['b']} - {$data['s']} = {$data['p']}" ;

}[/php]
Thank you for all help in advance.

Are you sure that you are even getting inside the if block here
[php]if (isset($_REQUEST[‘value’]))[/php]
I don’t see anything in your form that would come across to php of $_REQUEST[‘value’]. Really you should be using $_POST or $_GET and not $_REQUEST, $_REQUEST can have unexpected results.

calc.php
[php]<?php
function calculate($num1, $num2, $num3)
{
$data = array() ;

$data[‘c’] = $num1 / 50;
$data[‘b’] = $num2 / 12;
$data[‘s’] = $num3 / 5;
$data[‘p’] = $data[‘c’] + $data[‘b’] - $data[‘s’];

if($data['s'] > 4)
{
    $data['s']= 4;
}
return $data;

}

if (isset($_POST[‘submit’]) && $_POST[‘submit’] == ‘calc’)
{
$num1 = $_POST[‘num1’];
$num2 = $_POST[“num2”];
$num3 = $_POST[“num3”];

$data = calculate($num1, $num2, $num3);
$formula = "{$data['c']} + {$data['b']} - {$data['s']} = {$data['p']}";

}
?>

Untitled Document

<?php echo (isset($formula)) ? $formula : "Formula Calculator"; ?>

[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service