Jump to content

PHP: Very Basic Calculator

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
29 replies to this topic

#1
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
G'day everyone.

This was just a little project I made in one of my classes. The teacher teaches nothing and just gives us a .PDF full of VB and HTML code. It only works in IE anyway. But I went off did my own in PHP.

Also sorry to chili, in his thread I mentioned the teacher used ASP.NET. That what was originally but then he changed to online VB, that's my fault sorry.

This script was made in about 60 minutes and the teacher is really easy so I made it as basic as possible.

There are two files.
Here is the first one.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Mathematical Calculator</title>

</head>


<body>


<center>

<form method="POST" action="action.php">

	<p>First number: <input type="text" name="val1" /> <br />

    Second number: <input type="text" name="val2" /><br />

    

    <input type="radio" name="opt" value="add"/> Addition <br />

    <input type="radio" name="opt" value="sub"/> Subtract <br />

    <input type="radio" name="opt" value="mul"/> Multiply <br />

    <input type="radio" name="opt" value="div"/> Divide

    </p>

    

    <p>

    To the power of: <input type="text" name="powernum" /><br />

    

    <input type="radio" name="power" value="power2"/> Power of 2 <br />

    <input type="radio" name="power" value="power3"/> Power of 3

    </p>

    <input type="submit" value="Submit" name="submit" />

</form>

</center>


</body>

</html>


Now here is the second.

<?php

$val1 = $_POST['val1'];

$val2 = $_POST['val2'];

$option = $_POST['opt'];

$powerval = $_POST['power'];

$powernum = $_POST['powernum'];

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Mathematical Calculator</title>

</head>


<body>


<?php

if ($option == add) {

	$answer = $val1 + $val2;

}

if ($option == sub) {

	$answer = $val1 - $val2;

}

if ($option == mul) {

	$answer = $val1 * $val2;

}

if ($option == div) {

	$answer = $val1 / $val2;

}

if ($powerval == power2) {

	$answerpower = $powernum * $powernum;

}

if ($powerval == power3) {

	$answerpower = $powernum * $powernum * $powernum;

}


echo "<br />";

if ($option == add) {

	echo "$val1 + $val2 <br />";

	echo "$answer";

}

if ($option == sub) {

	echo "$val1 - $val2<br />";

	echo "$answer";

}

if ($option == mul) {

	echo "$val1 * $val2<br />";

	echo "$answer";

}

if ($option == div) {

	echo "$val1 / $val2<br />";

	echo "$answer";

}

echo "<br />";

if ($powerval == power2) {

	echo "$powernum * $powernum<br />";

	echo "$answerpower";

}

if ($powerval == power3) {

	echo "$powernum * $powernum * $powernum<br />";

	echo "$answerpower";

}

?>


<center>

<form method="POST" action="action.php">

	<p>First number: <input type="text" name="val1" /> <br />

    Second number: <input type="text" name="val2" /><br />

    

    <input type="radio" name="opt" value="add"/> Addition <br />

    <input type="radio" name="opt" value="sub"/> Subtract <br />

    <input type="radio" name="opt" value="mul"/> Multiply <br />

    <input type="radio" name="opt" value="div"/> Divide

    </p>

    

    <p>

    Number: <input type="text" name="powernum" /><br />

    

    <input type="radio" name="power" value="power2"/> Power of 2 <br />

    <input type="radio" name="power" value="power3"/> Power of 3

    </p>

    <input type="submit" value="Submit" name="submit" />

</form>

</center>




</body>

</html>


This is just a basic one as I explained above.

You input two values and select a radio button. You then click on the submit button and it show the operation and the answer of what you chose.

For more a advanced one check out Chili5's PHP Calculator Script here.

#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Also a live demo can be seen here

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
That's a great simple script. Like it :D That's how mine started. I have to say yours looks so much better than mine visually. :D

Anyways ways to improve:

1. prevent dividing by zero :)

if ($powerval == power2) {
    echo "$powernum * $powernum<br />";
    echo "$answerpower";
}
if ($powerval == power3) {
    echo "$powernum * $powernum * $powernum<br />";
    echo "$answerpower";
} 

Instead of doing that, make a function that calls the pow function. That is a lot of repetitive code there.

There is a lot of ways to improve this actually. :)

#4
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
I know there is heaps of ways. But I only had one lesson to think of something to make and then make it.

I will edit it when I got some time.

#5
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
How come I can't even edit my own post?
Sorry for double posting, the edit button is showing up.

I have wrote a Part I tutorial on this script on how to do it. I just finished editing the code for Part II I just need to write the post for it.

What I changed was put the variables in an include file. Added support for any exponent using the pow() function. And instead of having to check the $option variable twice, once for the answer and the second for echoing the answer, it's in one now.

Next I will work on functions and putting them into a include.
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!


#6
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I don't know but theirs a time limit that you are allowed to edit I think.

Great, can't wait to see what you got. :)

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
There is a time limit on editing in these sections. You can make a new thread though, Calc Imrpoved! :)

#8
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
But I will be making so many different versions, it will be useless making so many different threads.
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!


#9
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Well don't post every new version you make, just when you make a significant change. :D

#10
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
OK will do mate, thanks for that :)
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!


#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
If you make new threads, you get more points. ;)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#12
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
hehe, Your obsessed xD I will catch you sooner or later. But I just completely re-made the script.
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!