Is this good coding?
Posted: Sat Sep 23, 2006 11:45 am
Would someone mind reviewing my code and tell me if that's as best I could do a calculator. Are there are advanced methods of doing such a project?
http://stesbox.co.uk/php/adcalc.php Preview
Regards,
Code: Select all
<form method="post"
action="calc1.php">
<center><input type="text"
name="valOne"
value=""><br>
<input type="radio"
name="calc"
value="add"/> Add<br>
<input type="radio"
name="calc"
value="subtract"/> Subtract<br>
<input type="radio"
name="calc"
value="divide"/> Divide <br>
<input type="radio"
name="calc"
value="multiply"/> Multiply <br>
<input type="text"
name="valTwo"
value=""><br>
<input type="submit"
value="=">
</form>
<?php
$valOne = $_REQUEST["valOne"];
$valTwo = $_REQUEST["valTwo"];
$calculation = $_REQUEST["calc"];
function add($valOne, $valTwo) {
return $valOne + $valTwo;
}
function subtract($valOne, $valTwo) {
return $valOne - $valTwo;
}
function divide($valOne, $valTwo) {
return $valOne / $valTwo;
}
function multiply($valOne, $valTwo) {
return $valOne * $valTwo;
}
if ($calculation == "add") {
$equals = add($valOne, $valTwo);
?><h2><?php
echo "$valOne $calculation $valTwo = $equals";
$firstVisit == "no";
}
if ($calculation == "subtract") {
$equals = subtract($valOne, $valTwo);
?><h2><?php
echo "$valOne $calculation $valTwo = $equals";
$firstVisit == "no";
}
if ($calculation == "divide") {
$equals = divide($valOne, $valTwo);
?><h2><?php
echo "$valOne $calculation $valTwo = $equals";
$firstVisit == "no";
}
if ($calculation == "multiply") {
$equals = multiply($valOne, $valTwo);
?><h2><?php
echo "$valOne $calculation $valTwo = $equals";
$firstVisit == "no";
}
?>Regards,