pocobueno1388 Posted July 14, 2007 Share Posted July 14, 2007 Hello =] I am trying to compare two numbers. In a previous post Frost told me that comparing two numbers like this would always return true: if (12 < 3) So she gave me a function: <?php function compare($val1, $val2, $condition) { switch ($condition) { case '>': if ($val1 > $val2) { return true; } break; } return false; } ?> I am using the function like this: <?php if (compare($new_amount, $row['deposit'], '>')) echo "ERROR!"; ?> So if $new_amount is greater than $row['deposit'] I would like to throw an error. The problem is that it ALWAYS throws an error, even if $new_amount is less than $row['deposit']. Where did I go wrong? Any help is greatly appreciated, thanks Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted July 14, 2007 Share Posted July 14, 2007 I've tested your code and it works just fine here... Maybe it has something to do with $new_amount and $row['deposit'] If you can't get the code you posted to work try this, even though it's almost identical to your own. I've tested this too and it works... <?php function compare($val1, $val2, $condition) { switch ($condition) { //$val1 less than $val2 case '<': if($val1 < $val2){ return true; } break; //$val1 greater than $val2 case '>': if($val1 > $val2){ return true; } break; //$val1 less than or equal to $val2 case '<=': if($val1 < $val2){ return true; } break; //Add more if you like... } } //Test the function if(compare(1,8,"<=")) { echo "Returned true..."; } else { echo "Returned false..."; } ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 14, 2007 Author Share Posted July 14, 2007 I just echoed this out: <?php echo "compare($new_amount, {$row['deposit']}, '>')"; ?> And this was the result: compare(5005, 5000, '>') So I don't think it is a problem with my numbers... Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted July 14, 2007 Share Posted July 14, 2007 Nope... the numbers look fine. I just asked because I sometime miss the simplest things my self It's strange that your code worked for me. Have you tried the code I posted? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 14, 2007 Author Share Posted July 14, 2007 The code you posted is the same as mine...the only difference is you added new operators to the function. I only need that one operator, so I have no use for the other ones =/ Do you think the switch statement expects at least two cases, and thats why it isn't working? Let me try it. EDIT: Nope, that didn't work. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted July 14, 2007 Share Posted July 14, 2007 It has one difference, no "return false" after all the cases in my code But it shouldn't matter anyway because it will never reach that line due to break... If you only need that one statement why don't you: if($new_amount > $row['deposit']) { echo "Error..:"; } And to get back to your first post, if(2 > 3) will not return true, I can promise you that... Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 14, 2007 Author Share Posted July 14, 2007 And to get back to your first post, if(2 > 3) wont return true, I can promise you that... No, it really does =/ I'm not sure why. Take a look at this post: http://www.phpfreaks.com/forums/index.php/topic,146747.msg628482.html#msg628482 And I was trying the IF statement, but it was returning true even when it wasn't. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted July 14, 2007 Share Posted July 14, 2007 If I run this on my server it get "2 is not greater than 3": <?php if(2 > 3) { echo "2 is greater than 3"; } else { echo "2 is not greater than 3"; } ?> And this: <?php $a = 4; $b = 5; if($a > $b) { echo "$a is greater than $b"; } else { echo "$a is not greater than $b"; } ?> Results in "4 is not greater than 5" being printed in my browser... Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 14, 2007 Author Share Posted July 14, 2007 I'm an idiot...I didn't do the correct math with my $new_amount variable, so it was coming out differently than I thought. So everything is working good now. I have no idea what to think about comparing two numbers now...as you can see in the post I showed you, it clearly wasn't working. Well, thanks for helping me through this impossible to answer question haha Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted July 14, 2007 Share Posted July 14, 2007 Btw this topic http://www.phpfreaks.com/forums/index.php/topic,145271.html does not speak about <?php if(1 > 2) { echo "true"; } else { echo "false"; } ?> nor <?php if($a > $b) { echo "true"; } else { echo "false"; } ?> but <?php $rule = "1>2"; if($rule) { echo "true"; } else { echo "false"; } ?> The last code piece, if($rule) will always return true. Think about when you (ppl in general) do when they want to test if a variable is set, exactly that... if($var) and it returns true if $var (or $rule) is anything but false or 0... So the point of that topic (or one of them) is that you can't create "on the fly" if-statements as easy as that. - - - - - - - - - About comparing numbers you have to think this: I compare $a and $b by doing an if-statement like so: if($a > $b), if($a <= $b), if($a == $b), if($a != $b) Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Author Share Posted July 15, 2007 Ah, I completely understand now...thanks for clearing that up. I should have re-read my other topic, because I spaced out that I was doing that "on the fly". Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.