PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
It is currently Thu Dec 03, 2020 5:46 pm

All times are UTC - 5 hours

Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Mon Jul 19, 2010 10:50 am 
Forum Newbie

Joined: Mon Jul 19, 2010 10:15 am
Posts: 1
Right, hello everybody. I thought i'd kill two birds with one stone in this post. I have just started learning PHP, so please bear with me.

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:

Syntax: [ Download ] [ Hide ]

$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>';




if($_POST['_submit_check_']){  //Main logic if statement. THE PROBLEM CODE
        if($formErrors = validateForm()){
        } else{
}else {
        } //end of if

function showForm($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";




The problem I am having is that the answer (to the calculation carried out by the program) is shown to the user on a separate page to the form. So, when the user enters valid numbers and clicks the 'Calculate' button to submit the form elements, the answer is displayed, on its own (without form), on a separate page. I want the answer to be displayed underneath the form on the same page. I have identified the piece of code that is causing this (shown by the comment "main logic if statement" in the above code). If I call the processForm() and showForm() functions without the if statement shown, the answer is displayed on the same page, just the way I want it (however, obviously, there is no validation without the if statement. If I type in valid numbers and click submit, without the if statement but just by calling the functions, it shows the answer on the same page - see comments just before if statement in question). As soon as I include that if statement though, the answer is displayed on a separate page. Could someone please tell me why this is the case? I'm guessing it is something to do with the $_POST array (???). How can I get around this but still use the/a if statement?

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 :banghead:

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 33 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group