G'day everyone. As you can see I have posted my Very Basic Calculator script, I didn't like the code since I only had a short time to make it. Today I have been working on this and I have fixed up a fair bit of the code.
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
variables.phpCode:<?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";
}
?>
index.phpCode:<?php
$option = $_POST['opt'];
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$ansformat = number_format($ans, 0, '.', ',');
$ans2format = number_format($ans2, 0, '.', ',');
?>
form.phpCode:<?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>";
}
?>
Code:<?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
}
?>
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
To display a message when a number is left empty use:
Also you know why the number_format thing ain't working now. That's a great idea.Code:if (trim($_POST['num1']) == "") {
echo "Number 1 cannot be empty. Defaulting to 1.";
$num1 = 1;
}
![]()
Nice job![]()
Thanks for the tip mate, I will add more things like that tomorrow after school. hehe. I took things from you and you take things from me. This is an example of sharing![]()
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
Ooh terrible center tags.lol
Excellent work on this calculator. Can't wait to see more of it.![]()
i was hoping that someone would teach me how to create calculator using visual basic programming language version 6.0
I would look around on google for one. This topic is about a PHP calculator, and actually making calculators are really easy. After you learn the language, it is simple.
It would be a useless language if it didn't. lol
Why don't you add square root? Theirs a sqrt() function you can use.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks