The only remaining issue is that when I try and complete a second computation using the value from the previous calculation as the beginning value, the answer is always 0. What else needs to be done in order to just continuously compute equations?
<html>
<?php
session_start();
$_SESSION['num1']= 0;
$num1 = $_SESSION['num1'];
if(isset($_POST['num1'])){
$num1 = $_POST['num1'];
}
if(isset($_POST['num2'])){
$num2 = $_POST['num2'];
}
if(isset($_POST['calcu'])){
$calcu = $_POST['calcu'];
}
function calculate($n1,$n2,$calcu){
switch($calcu)
{
case "Addition":
$compute = $n1 + $n2;
break;
case "Subtraction":
$compute = $n1 - $n2;
break;
case "Multiplication":
$compute = $n1 * $n2;
break;
case "Division":
$compute = $n1 / $n2;
break;
}
return $compute;
}
// echo "$calcu <br /> <br /> Beginning Number: $num1 <br /> Next Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2, $calcu);
$_SESSION['num1']=calculate($num1, $num2, $calcu);
?>
<body>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
Beginning Number:<?php echo $_SESSION['num1'];?><br>
Next Number: <input type="number"name="num2"><br>
Calculation: Please type one of the following- Addition, Subtraction, Multiplication, or Division. <input type="text" name="calcu"><br>
<input type="submit"><br>
</form>
</body>
</html>
From your code:
$_SESSION['num1']= 0;
$num1 = $_SESSION['num1'];
Since you've just assigned 0 to $_SESSION['num1'], $num1 will always end up zero as well.
You probably want to wrap that first assignment in a conditional:
if (!isset($_SESSION['num1'])) {
$_SESSION['num1'] = 0;
}
Related
I was trying to make a calculator and wanted to use Arithmetic Operators in the switch Statement but i am not able to.
Can someone help me out.
Error:
Warning: Undefined array key "num2" in on line 16
220
Fatal error: Uncaught DivisionByZeroError: Division by zero in 27 Stack trace: #0 {main} thrown on line 27
<!DOCTYPE html>
<html>
<body>
<form action="php.php" method="post">
Number 1: <input type="number" name="num1"><br>
Operator: <input type="text" name="op"><br>
Number 3: <input type="number" name="num1"><br><br>
<input type="submit">
</form>
<?php
$num1 = $_POST["num1"];
$op = $_POST["op"];
$num2 = $_POST["num2"];
switch($op)
{
case "+":
echo $num1 + $num2;
case "-":
echo $num1 - $num2;
case "*":
echo $num1 * $num2;
case "/":
echo $num1 / $num2;
}
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<form action="php.php" method="GET">
Number 1: <input type="number" name="num1"><br>
Operator: <input type="text" name="op"><br>
Number 3: <input type="number" name="num2"><br><br>
<input type="submit">
</form>
<?php
$num1 = $_GET['num1'];
$op = $_GET['op'];
$num2 = $_GET['num2'];
switch($op)
{
case '+':
echo $num1 + $num2;
break;
case '-':
echo $num1 - $num2;
break;
case '*':
echo $num1 * $num2;
break;
case '/':
echo $num1 / $num2;
break;
}
?>
</body>
</html>
Put break after every case
These was a naming error in the code with the names of the variables. eg num1 and num2. Name them correctly
I'm making a simple Calculator in PHP and want to create validation for a divide by 0.
At the moment when a user divides by zero, PHP issues the warning Warning: Division by zero in
I know I need to check if y == 0 if the user is doing division, but I'm unsure where to put it in my code.
<html>
<head>
<title>PHP Calculator</title>
</head>
<body>
<h3>PHP Calculator (Version 6)</h3>
<p>Add, subtract, divide or multiply and output the result</p>
<form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">
<input type = "number" name = "x" placeholder = "0" required>
<select name = "operator">
<option>None</option>
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type = "number" name = "y" placeholder = "0" required>
<input type="submit" name="submit" value="Calculate"/>
</form>
<p>The answer is: </p>
<?php
if (isset($_GET['submit'])) {
$x = $_GET['x'];
$y = $_GET['y'];
$operator = $_GET['operator'];
switch ($operator) {
case "+":
echo $x + $y;
break;
case "-":
echo $x - $y;
break;
case "*":
echo $x * $y;
break;
case "/":
echo $x / $y;
break;
default:
echo "You should to select a method!";
break;
}
}
?>
</body>
</html>
I would put it in the switch statement in division case,
case "/":
echo $y==0 ? 'Illegal divisor' : ($x / $y);
break;
This will ensure readability of your code.
If you're not familiar with the ternary operator here's the syntax:
{condtion} ? {true statement} : {false statement}
This is the code that I'm using. All the arithemetic operations are done successfully except subtraction. When Subtraction is selected, it gives the error. If I use two different echo statements, one for "The answer is: " and the other echo statement for the subtraction between two variables, it gives no error. What is the reason behind the generation of this error when both the variables string are printed using one echo statement??
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form>
<input type="text" name="num1" placeholder="First Number">
<input type="text" name="num2" placeholder="Second Number">
<select name="operator">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<br>
<button name="submit" value="submit">Calculate</button>
</form>
<?php
if (isset($_GET['submit'])) {
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator) {
case "None":
echo "You need to set a method!";
break;
case 'Add':
echo "The answer is: ".$num1 + $num2;
break;
case 'Subtract':
echo "The answer is: ".$num1 - $num2;
break;
case 'Multiply':
echo "The answer is: ".$num1 * $num2;
break;
case 'Divide':
echo "The answer is: ".$num1 / $num2;
break;
}
}
?>
</body>
</html>
https://www.php.net/manual/en/language.operators.precedence.php
The . in your string concatenation has a higher operator precedence than your -. You should add parentheses around your $num expressions.
switch ($operator) {
case "None":
echo "You need to set a method!";
break;
case 'Add':
echo "The answer is: ".($num1 + $num2);
break;
case 'Subtract':
echo "The answer is: ". ($num1 - $num2);
break;
case 'Multiply':
echo "The answer is: ".($num1 * $num2);
break;
case 'Divide':
echo "The answer is: ".($num1 / $num2);
break;
}
I tried to make a quadratic equation solver in php:
index.html:
<html>
<body>
<form action="findx.php" method="post">
Find solution for ax^2 + bx + c<br>
a: <input type="text" name="a"><br>
b: <input type="text" name="b"><br>
c: <input type="text" name="c"><br>
<input type="submit" value="Find x!">
</form>
</body>
</html>
findx.php:
<?php
if(isset($_POST['a'])){ $a = $_POST['a']; }
if(isset($_POST['b'])){ $b = $_POST['b']; }
if(isset($_POST['c'])){ $c = $_POST['c']; }
$d = $b*$b - 4*$a*$c;
echo $d;
if($d < 0) {
echo "The equation has no real solutions!";
} elseif($d = 0) {
echo "x = ";
echo (-$b / 2*$a);
} else {
echo "x1 = ";
echo ((-$b + sqrt($d)) / (2*$a));
echo "<br>";
echo "x2 = ";
echo ((-$b - sqrt($d)) / (2*$a));
}
?>
the problem is that it's returning wrong answers (d is right,x1 and x2 are not) seems like sqrt() is returning zero or maybe something else.
There's a typo in this line:
elseif($d = 0)
which is assigning the value 0 to $d instead of comparing it. That means you are always evaluating sqrt(0), which is 0, in your else block.
It should be:
elseif($d == 0)
i am currently doing some simple calculator for a practice but it the output or result is not showing here is my code guys hope you can help me :/
<input type="radio" value= "Addition" name="calcu"> Addition .<br />
<input type="radio" value= "Subtraction" name="calcu"> Subtraction .<br />
<input type="radio" value= "Multiplication" name="calcu"> Multiplication .<br />
<input type="radio" value= "Division" name="calcu"> Division .<br />
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$calcu = $_POST['calcu'];
function calculate($n1,$n2)
{
switch('$calcu')
{
case "Addition";
$compute = $n1 + $n2;
break;
case "Subtraction";
$compute = $n1 - $n2;
break;
case "Multiplication";
$compute = $n1 * $n2;
break;
case "Division";
$compute = $n1 / $n2;
break;
}
}
echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2);
?>
Here is complete code:
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$calcu = $_POST['calcu'];
function calculate($n1,$n2, $calcu) // set $calcu as parameter
{
switch($calcu)
{
case "Addition": // here you have to use colons not semi-colons
$compute = $n1 + $n2;
break;
case "Subtraction":
$compute = $n1 - $n2;
break;
case "Multiplication":
$compute = $n1 * $n2;
break;
case "Division":
$compute = $n1 / $n2;
break;
}
return $compute; // returning variable
}
echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2, $calcu); // you need to pass $calcu as argument of that function
?>
Change switch('$calcu') to switch($calcu). It should be this way.
But not only that. Your variables are undefined because you are trying to address them before form is submited, i.e they don't exist yet.
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$calcu = $_POST['calcu'];
And there you address them
echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2);
The right way to implement this is to check if form was submitted:
<input type="radio" value= "Addition" name="calcu"> Addition .<br />
<input type="radio" value= "Subtraction" name="calcu"> Subtraction .<br />
<input type="radio" value= "Multiplication" name="calcu"> Multiplication .<br />
<input type="radio" value= "Division" name="calcu"> Division .<br />
<?php
if (isset($_POST)){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$calcu = $_POST['calcu'];
function calculate($n1,$n2)
{
switch('$calcu')
{
case "Addition";
$compute = $n1 + $n2;
break;
case "Subtraction";
$compute = $n1 - $n2;
break;
case "Multiplication";
$compute = $n1 * $n2;
break;
case "Division";
$compute = $n1 / $n2;
break;
}
}
echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2);
unset($_POST);
}
?>
Change switch('$calcu') to switch($calcu).
As #PeterM mentioned, you are accessing variable $calcu out of scope. Either you pass the $calcu variable to fun calculate or access directly by $_POST array.
use switch($_POST['calcu']).
OR
function calculate($n1,$n2, $calcu) {
...
}
Call the fun by calculate($n1,$n2, $calcu).