Reputation: 13
I'm just doing a rather lame calculator as I am beginning to learn PHP. I can't figure out why everything seems to be fine except the answer will not display.
Here's the HTML:
<head>
<meta charset="utf-8">
<title>A (Seriously) Simple Calculator</title>
<link rel="stylesheet" type="text/css" href="./calc_css.css">
</head>
<body>
<form method="post" attribute="post" action="calc1.php">
<p>First Value:<br/>
<input type="number" id="first" name="first" step="0.0000000001"></p>
<p>Second Value:<br/>
<input type="number" id="second" name="second" step="0.0000000001"></p>
<p>+<input type="radio" name="operation" id="add" value="add" checked="true"></p><br/>
<p>-<input type="radio" name="operation" id="subtract" value="subtract"></p><br/>
<p>X<input type="radio" name="operation" id="multiply" value="multiply"></p><br/>
<p>/<input type="radio" name="operation" id="divide" value="divide"></p><br/>
<p></p>
<button type="submit" name="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>
And here's my PHP:
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = floatval($_POST['first']);
$second = floatval($_POST['second']);
if($_POST['operation'] == 'add') {
echo $first + $second;
}
else if($_POST['operation'] == 'subtract') {
echo $first - $second;
}
else if($_POST['operation'] == 'multiply') {
echo $first * $second;
}
else($_POST['operation'] == 'divide') {
echo $first / $second;
}
?>
</p>
</body>
</html>
I don't think it has to do with my input step or type, and I've tried all manner of things (that I can think of) in my PHP file. That said, I am a very green beginner. Any help would be greatly appreciated.
Upvotes: 1
Views: 524
Reputation: 7911
There was an error in your code, look at your last "else" statement in your PHP code.
<?php
if($_POST['operation'] == 'add') {
echo $first + $second;
}
else if($_POST['operation'] == 'subtract') {
echo $first - $second;
}
else if($_POST['operation'] == 'multiply') {
echo $first * $second;
}
else if($_POST['operation'] == 'divide') {
if($second == 0){
echo 'Cannot divide by 0';
} else {
echo $first / $second;
}
} else {
echo 'unknown operator';
}
?>
Also keep in mind that you cannot divide by 0, as it will create a warning message in PHP.
So its best to enable error_reporting when writing and testing your code shown in "Fred -ii-" answer.
Upvotes: 3
Reputation: 74232
The problem is with your last conditional statement being else
instead of another else if
.
else($_POST['operation'] == 'divide') {
echo $first / $second;
}
Having error reporting set to catch and display, would have thrown you something like this:
Parse error: syntax error, unexpected '{' in /var/usr/you/folder/file.php on line 24
Change it to:
else if($_POST['operation'] == 'divide') {
echo $first / $second;
}
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
That notice will be thrown regardless of which operation is chosen.
else{...}
is used as an exit if something is not set or does meet a criteria in a comparison statement, like if
and else if
:
It's assuming that else($_POST['operation'] == 'divide')
will always compare to "divide" rather than else { $var=x; }
being an "assign this variable to "x", in turn throwing you an error.
From the manual: http://php.net/manual/en/control-structures.elseif.php
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
Other references:
Upvotes: 5