I have made a very basic calculator program where users enter numbers, choose the operator, click 'Calculate', a value is calculated and then displayed to the user on screen. The code is below:
Code: Select all
<?php
$operators = array('*','/', '-', '+');
function buildMenu($operators){
print '<td><select name="operator">';
foreach($GLOBALS['operators'] as $operator){
print '<option name="$operator"';
if($operator == $_POST['operator']){
print ' selected="selected">';
print $operator;
print '</option>';
} else{
print '>';
print $operator;
print '</option>';
}
}
print '</select></td>';
}
//showForm();
//processForm();
//IF I COMMENT OUT THE BELOW IF STATEMENT AND UNCOMMENT THE ABOVE TWO LINES, THE ANSWER IS DISPLAYED ON THE SAME PAGE, AS I WANT IT.
if($_POST['_submit_check_']){ //Main logic if statement. THE PROBLEM CODE
if($formErrors = validateForm()){
showForm($formErrors);
} else{
processForm();
}
}else {
showForm();
} //end of if
function showForm($errors = ''){
if($errors){
$errorText = '<tr><td>Errors were found when trying to complete the calculation. Please correct the following errors:';
$errorText .= '</td><td><ul><li>';
$errorText .= implode('</li><li>', $errors);
$errorText .= '</li></ul></td></tr>';
} else {
$errors = '';
}
print '<table cellpadding="10">';
print '<form method="post" action="'. $_SERVER['PHP_SELF'] .'"';
print '<tr><td>First Operand:</td>';
print '<td><input type="text" name="firstoperand" value="';
print htmlspecialchars($_POST['firstoperand']) .'"/></td>';
print '<tr><td>Operator:</td>';
print buildMenu($GLOBALS['operators']);
print '<tr><td>Second Operand:</td>';
print '<td><input type="text" name="secondoperand" value="';
print htmlspecialchars($_POST['secondoperand']).'"/></td>';
print '<tr><td><input type="submit" name="submit" value="Calculate"/></td></tr>';
print '<input type="hidden" name="_submit_check_" value="1"/>';
print '</form>';
print '</table>';
print $errorText;
}
function validateForm(){
if(! strlen($_POST['firstoperand'])){
$errors[] = 'Please enter a number into first text box';
} else if(! strval(floatval($_POST['firstoperand'])) == $_POST['firstoperand']){
$errors[] = 'Please enter a number into first text box';
}
if(! strlen($_POST['secondoperand'])){
$errors[] = 'Please enter a number into second text box';
}else if(! strval(floatval($_POST['secondoperand'])) == $_POST['secondoperand']){ //is this a number???
$errors[] = 'Please enter a number into second text box';
} //end of code in question
if(! in_array($_POST['operator'],$GLOBALS['operators'])){
$errors[] = 'Please select a valid operator';
}
return $errors;
}
function processForm(){
if($_POST['operator'] == '*'){
$total = ($_POST['firstoperand']) * ($_POST['secondoperand']);
}
if($_POST['operator'] == '/'){
$total = ($_POST['firstoperand']) / ($_POST['secondoperand']);
}
if($_POST['operator'] == '-'){
$total = ($_POST['firstoperand']) - ($_POST['secondoperand']);
}
if($_POST['operator'] == '+'){
$total = ($_POST['firstoperand']) + ($_POST['secondoperand']);
}
print " $_POST[firstoperand] $_POST[operator] $_POST[secondoperand] = $total";
}
?>
Finally, the next thing is just a query really. The code with the comment "is this a number?" comment above works brilliantly. It returns an error if the input is not a number, fine. Clearly, the error message is shown when strval(floatval($_POST['firstoperand'])) does not equal $_POST['firstoperand'], as this means that, essentially, what has been entered is not just a number. This is fine. However, why does the error not show when there is nothing entered into the box when the form is submitted? In my mind, if nothing is entered, an empty string ("") will be entered into the $_POST array. However, when strval(floatval($_POST['firstoperand'])) is called, surely this evaluates to "0". Therefore, when we compare the two strings, they shouldn't be equal and so the error message should be displayed (in my mind!) as they are not the same ("0" is different to ""). The error message isn't shown however (hence why I added the strlen function, prior to the piece of code in question, to check that something has been inputted) which suggests that, actually, "0" is equal to "". I would just like to know, essentially, why "0" is equal to "" (if I have interpreted it correctly). The only thing that I could come up with was, both "0" and "" evaluate to false and therefore, they are both equal (i.e. false == false). However, if this was the case, why doesn't 47 == 56 evaluate to true (as 47 evaluates to true and so does 56, so true == true).
Thanks a lot for your help. Sorry if it's a bit difficult to understand. As I said, I am new to PHP and these things are just annoying me as I cannot answer them in a satisfactory way by myself at the moment