Jump to content

[SOLVED] Comparing two numbers, function not working.


pocobueno1388

Recommended Posts

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

Link to comment
Share on other sites

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...";
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

 

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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) ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.