I will write a tutorial for this one, but I would just like to see what everyone else thinks of this script. Most of the things you will see there isn't much of a difference to the layout but under the hood there is heaps.
I would like the thank everyone for commenting on my old script and a BIG thanks to Chili. He's script was the one that I got some ideas from and turned that old script into something better.
It now has an error shown when you try to divide by zero. It has support for any base and exponent when using power to. It uses functions instead of repeating if statements, I tried adding a 1000 seperater using the number_format() function but I couldn't get it to work. Also tried to add an error message when you don't select a number which I will work on the for the next version.
It has 3 include files but works on the one page for faster processing. It has a better code layout then before.
I hope use like it and enjoy my new script :)
NOTE: Link to Chili's Calculator.
Yet again thanks to him and everyone who helped me with my old script :)
Here are the files.
functions.php
<?php
function checkEmpty() {
global $option;
if (empty($option)) {
echo "You must choose what function you are going to be using.";
} else {
$option = $_POST['opt'];
}
}
function addNum() {
global $num1;
global $num2;
$ans = $num1 + $num2;
echo "$num1 + $num2 <br />";
echo "= $ans";
}
function subNum() {
global $num1;
global $num2;
$ans = $num1 - $num2;
$ans2 = $num2 - $num1;
echo "$num1 - $num2 <br />";
echo "= $ans <br /><br />";
echo "$num2 - $num1 <br />";
echo "= $ans2";
}
function mulNum() {
global $num1;
global $num2;
$ans = $num1 * $num2;
echo "$num1 * $num2 <br />";
echo "= $ans";
}
function divNum() {
global $num1;
global $num2;
if ($num2 == 0) {
echo "You can't divide by zero.";
} else {
$ans = $num1 / $num2;
$ans2 = $num2 / $num1;
echo "$num1 / $num2 <br />";
echo "= $ans <br /><br/>";
echo "$num2 / $num1 <br />";
echo "= $ans2";
}
}
function powNum() {
global $num1;
global $num2;
$ans = (pow($num1, $num2));
echo "$num1<sup>$num2</sup> <br />";
echo "= $ans";
}
?>
variables.php
<?php $option = $_POST['opt']; $num1 = $_POST['num1']; $num2 = $_POST['num2']; $ansformat = number_format($ans, 0, '.', ','); $ans2format = number_format($ans2, 0, '.', ','); ?>
index.php
<?php
require "functions.php";
require "form.php";
require "variables.php";
if(!isset($_POST['submit'])) {
formInput();
} else {
switch ($option) {
case "+":
addNum();
break;
case "-":
subNum();
break;
case "*":
mulNum();
break;
case "/":
divNum();
break;
case "^":
powNum();
break;
}
echo "<br /><a href='index.php'>Another calculation</a>";
}
?>
form.php
<?php
//This is the form code which will be shown on index.php
function formInput() {
?>
<center>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
First/Base number: <input type="text" name="num1" /> <br />
Second/Exponent number: <input type="text" name="num2" /><br />
<input type="radio" name="opt" value="+"/> Addition <br />
<input type="radio" name="opt" value="-"/> Subtract <br />
<input type="radio" name="opt" value="*"/> Multiply <br />
<input type="radio" name="opt" value="/"/> Divide <br />
<input type="radio" name="opt" value="^"/> Power
</p>
<input type="submit" value="Submit" name="submit" />
</form>
</center>
<?php
}
?>


Sign In
Create Account


Back to top









