I'm just learning about PHP and I've written this code to do operations given a form asking for two operands and a selected operation (add, subtract, multiply, or divide). For some reason, I've been getting this error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\Practice\forms\forms_3.php on line 52
Here's the code:
<?php
function input_operands(){
$form = <<<HTML
<table>
<tr>
<td><input type = "text" name ="'operand1'"></td>
<td><input type = "text" name ="'operand2'"></td>
</tr>
</table>
HTML;
print $form;
}
function input_operator(){
$form = <<<HTML
<table>
<tr><td>
<select name = "operation">
<option value = "add">Add</option>
<option value = "sub">Subtract</option>
<option value = "mul">Multiply</option>
<option value = "div">Divide</option>
</select>
</td</tr>
</table>
HTML;
print $form;
}
function display_form(){
print '<form method="POST" action = "' . $_SERVER[PHP_SELF] . '">';
input_operands();
input_operator();
print '<br><input type = "submit" value = "Compute">';
print '<input type = "hidden" name = "_submit_check" value = "1">';
print '</form>';
}
function calculate(){
if($_POST['operation'] == 'add') {
print $_POST['operand1'] . "+" . $_POST['operand2'] . '=' . intval($_POST['operand1']) + intval($_POST['operand2']);
}
if($_POST['operation'] == 'sub') {
print $_POST['operand1'] . "-" . $_POST['operand2'] . '=' . intval($_POST['operand1']) - intval($_POST['operand2']);
}
if($_POST['operation'] == 'mul') {
print $_POST['operand1'] . "*" . $_POST['operand2'] . '=' . intval($_POST['operand1']) * intval($_POST['operand2']);
}
if($_POST['operation'] == 'div') {
print $_POST['operand1'] . "/" . $_POST['operand2'] . '=' . floatval($_POST['operand1']) / intval($_POST['operand2']);
}
}
if(array_key_exists('_submit_check', $_POST)) {
calculate();
} else {
display_form();
}
?>
It's probably some really simple thing I'm missing here. Please help.
Thanks