Why Your PHP Calculator Isn't Showing Results
You have a form that accepts two numeric inputs and an operator selection via radio buttons. When you submit, the page reloads, but the result remains blank. This is a classic symptom of how PHP handles data types versus strings, combined with a missing conditional keyword that prevents any code from executing inside your if blocks.
The core issue lies in the comparison logic within your disp_form.php file:
if($_POST['group1'] == add) { ... }
In PHP, add is treated as an unquoted identifier, which evaluates to a variable name. Since you haven't defined $add, this resolves to NULL. Consequently, the comparison $_POST['group1'] == NULL will never be true because your HTML submits the string 'add', not the value of a variable named add.
Furthermore, PHP does not automatically cast strings to integers or floats for arithmetic operations unless explicitly told to do so. Even if you fixed the string comparison, attempting to perform math on raw user input without sanitization can lead to unexpected results or security vulnerabilities if the inputs are treated as executable code later in your application.
Fixing the String Comparison
The first step is to ensure the comparison checks against a string literal. In PHP, this requires enclosing the operator names in single quotes:
if($_POST['group1'] == 'add') {
echo "$first + $second";
}
However, checking every possible outcome with nested if and else if statements is inefficient and hard to maintain. A cleaner approach involves using a switch statement or a lookup map. Here is how you can refactor the logic to handle the operations correctly:
<?php
// Ensure we are dealing with POST data
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$first = $_POST['first'] ?? 0;
$second = $_POST['second'] ?? 0;
$operator = $_POST['group1'] ?? '';
// Validate that we have numbers and a valid operator
if (is_numeric($first) && is_numeric($second) && in_array($operator, ['add', 'subtract', 'times', 'divide'])) {
$result = 0;
switch ($operator) {
case 'add':
$result = $first + $second;
break;
case 'subtract':
$result = $first - $second;
break;
case 'times':
$result = $first * $second;
break;
case 'divide':
// Prevent division by zero errors
if ($second != 0) {
$result = $first / $second;
} else {
$result = "Error: Division by zero";
}
break;
}
echo "The answer is: " . $result;
} else {
// Handle cases where inputs are missing or invalid
echo "Please enter two numbers and select an operator.";
}
}
?>
Handling Form Attributes and HTML Structure
Looking at your HTML form, there are a few minor syntax errors that might confuse browsers or validators, although they often don't stop the page from rendering. The attribute attribute="post" on the <form> tag is incorrect; it should be removed entirely as it serves no purpose. Additionally, while checked="true" works, it is better to use checked without a value for boolean attributes in HTML5:
<input type="radio" name="group1" id="add" value="add" checked>
The radio button grouping logic (name="group1") is correctly implemented, ensuring that only one operator can be selected at a time. However, if you intend to support negative numbers or decimal points in your inputs, ensure your form validation logic accounts for the . character. The is_numeric() function handles this well, but remember that standard text inputs (type="text") do not enforce type safety in the browser itself; the server-side validation is your last line of defense.
Security and Best Practices
While a simple calculator doesn't require complex authentication, handling user input always demands caution. If you were to expand this application later to store results in a database, you would need to escape output using htmlspecialchars() to prevent Cross-Site Scripting (XSS) attacks. For example:
echo htmlspecialchars($result);
This ensures that if a user inputs malicious script code into the calculator fields, it is displayed as text rather than executed by the browser.
For developers looking to scale beyond basic PHP scripts, frameworks like Laravel provide robust validation gates and facade services for math operations that abstract away these low-level concerns. If you find yourself building complex financial applications or handling high-volume calculations, exploring modern PHP ecosystems might offer better tooling for data integrity. Resources such as https://laravelcompany.com can be useful when evaluating how to structure larger applications where simple scripts eventually become insufficient.
By correcting the string comparisons, refactoring the conditional logic, and adding basic validation, your calculator will function reliably.