Advertisement

Now Place:2uzhan.com » PHP Calculator Help

Web Development @ May 25, 2008 Views:0

Hello,

I am currently creating a PHP calculator, and I so far have been successful until this point. I want to include a exponent calculation feature, using the "pow()" function.

Also, I am just curious if there is a more efficient way to code this.

Here is my PHP source:

```
<?php
$number1 = $_POST['number1']; //First User Input Number
$number2 = $_POST['number2']; //Second User Input Number
$number3 = $_POST['number3']; //Third User Input Number
$number4 = $_POST['number4']; //Fourth User Input Number
$number5 = $_POST['number5']; //Fifth User Input Number
$operator = $_POST['operator']; //Math Operator
$exponents = $_POST['exponent']; //Takes the exponent input from the HTML form
$use_exponents = $_POST['use_exponents']; //Takes the exponent permission from the HTML form.
$exponent_calc = pow($number1, $exponents); // Calculates the exponent
if($use_exponents == "TRUE"){ //If the user gives permission then, use then return the exponent'd number
echo $exponent_calc;
}
if($use_exponents == "FALSE"){ //If false, then perform basic arithmetic.
echo "The result is: ", $result;
}
if($operator == "add"){ //NOTE: The bugfixes necessary for multiplication and division are unnecessary in addition and subtraction, as it's perfectly acceptable to use NULL values in addition and subtraction.
$result = $number1 + $number2 + $number3 + $number4 + $number5;
}
if($operator == "subtract"){ //NOTE: The bugfixes necessary for multiplication and division are unnecessary in addition and subtraction, as it's perfectly acceptable to use NULL values in addition and subtraction.
$result = $number1 - $number2 - $number3 - $number4 - $number5;
}
if($operator == "multiply"){
/* BEGIN BUGFIX: Multiplication by zero.
Since it is not possible to multiply by zero, and PHP chokes when it trys to, this bugfix was created to fix the issue. It is setup to change "0" to "1"
as it will not change the end result, but will make it so PHP can calculate the user's input.
*/
//Start for number3 bugfix.
if($_POST['number3'] == ""){ //if $number3 lacks a value (default value, unless the user enters a custom input, disregard it, and change it to "1", to avoid errors)
//getting PHP "Cannot Multiply by Zero" errors.
$num3 = "1";
}
else{
$num3 = $_POST['number3']; //if user input is anything but NULL, then use the user's input.
}
//Start for number4 bugfix.
if($_POST['number4'] == ""){ //same as $number3 bugfix, but it involves the $number4 variable.
$num4 = "1";
}
else{
$num4 = $_POST['number4']; //if user input is anything but NULL, then use the user's input.
}
//Start for number5 bugfix.
if($_POST['number5'] == ""){ //same as $number3 and $number4 bugfix, but it involves the $number5 variable.
$num5 = "1";
}
else{
$num5 = $_POST['number5']; //if user input is anything but NULL, then use the user's input.
}
/* END OF BUGFIX */
$result = $number1 * $number2 * $num3 * $num4 * $num5;
}
if($operator == "divide"){
/* BEGIN OF BUGFIX: Division by Zero
Since it is impossible to divide by zero, this bugfix changes the "0" to "1". While this will not effect the end answer, it allows the problem to
calculate
*/
//Start for number3 bugfix.
if($_POST['number3'] == ""){
$num3 = "1";
}
else{
$num3 = $_POST['number3'];
}
//Start for number4 bugfix.
if($_POST['number4'] == ""){
$num4 = "1";
}
else{
$num4 = $_POST['number4'];
}
//Start for number5 bugfix.
if($_POST['number5'] == ""){
$num5 = "1";
}
else{
$num5 = $_POST['number5'];
}
/* END OF BUGFIX */
$result = $number1 / $number2 / $num3 / $num4 / $num5;
}
```

And here is my HTML source:

```
<html>
<head> </head>
<body>
<h1>Ryan Leaf Research Calculator</h1>
<p> When you are entering numbers, do not add zeros to the boxes that you don't need. Just leave them blank</p>
<form action="calc.php" method="post">
<p><b>Number 1:</b> <input type="text" name="number1" size="30"></p>
<p><b>Number 2:</b> <input type="text" name="number2" size="30"></p>
<p><b>Number 3:</b> <input type="text" name="number3" size="30"></p>
<p><b>Number 4:</b> <input type="text" name="number4" size="30"></p>
<p><b>Number 5:</b> <input type="text" name="number5" size="30"></p>
<p><b>Exponent:</b> <input type="text" name="exponents" size="8"> Use Exponents?: <b> Yes </b> <input type="radio" name="use_exponents" value="TRUE"><b> No</b> <input type="radio" name="use_exponents" value="FALSE"></p>
<p>Math Operator:
<b> Add</b><input type="radio" name="operator" value="add" />
<b> Subtract</b><input type="radio" name="operator" value="subtract" />
<b> Mutliply </b><input type="radio" name="operator" value="multiply" />
<b> Divide</b><input type="radio" name="operator" value="divide" />
</p>
<input type="submit" value="Calculate">
</form>
</body>
</html>
```

Thank You,

Ryan Leaf

--------------Solutions-------------

When a user enters the page in question, a textarea is populated from data from a text file stored on the server. The user can then edit that text (add, ...

I'm attempting to simplify my fundraising page, and would like to show exactly how much each donation would help us out. I've listed the options below, but I would like ...

I have a list.php which displays all the records that is in the database with 2 options Edit or Delete. When clicked its suppose to delete by id but instead ...

Tags:php

- 1 [RESOLVED] dynamic list population
- 2 Best way to anonymize an email address
- 3 Please suggest simple accounting software
- 4 CRUD from gridview to
- 5 onClick in html forms
- 6 Delphi To Powerpoint.
- 7 MS Access validation rule help
- 8 Terminal Command To Start A Program Fully Maximized
- 9 'INDENTIFIED BY' causing error
- 10 Overriding max_allowed_packet set in my.ini by using "SET SESSION..." doesn't work

© 2018 2uzhan.com Contact