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
Related
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 have the bellow php code:
<form method="get">
<input type="text" name="num1" placeholder="num1">
<input type="text" name="num2" placeholder="num2">
<button type="submit" name="submit" value="func1">Submit</button>
</form>
The result is: <?php
if (isset($GLOBALS['result'])) {
echo $GLOBALS['result']; // there do not get the add result.
}
?>
<?php
$GLOBALS['result'] = 0;
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$GLOBALS['result'] = $num1 + $num2;
}
?>
The issue is if (isset($GLOBALS['result'])) {...} the code did not walk through.
Where is my issue?
Or how can I use a variables in different php tags?
Try below code
It is working.
<?php
session_start();
?>
<form method="get">
<input type="text" name="num1" placeholder="num1">
<input type="text" name="num2" placeholder="num2">
<button type="submit" name="submit" value="func1">Submit</button>
</form>
<?php
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$_SESSION['result'] = ($num1 + $num2);
}
?>
The result is: <?php
if (isset($_SESSION['result'])) {
echo $_SESSION['result']; // there do not get the add result.
}
?>
The page gets refreshed after submitting the form.
So the value get reset in GLOBALS.
If you can echo GLOBALS inside the submit condition then you will get the values.
Note : Use SESSION instead of GLOBALS.
<?php
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$GLOBALS['result'] = $num1 + $num2;
echo $GLOBALS['result'];
}
?>
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;
}
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).