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

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

?>

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.

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

 

 

 

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.

 

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

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

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

Archived

This topic is now archived and is closed to further replies.

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