pocobueno1388 Posted June 25, 2007 Share Posted June 25, 2007 I have a variable named "statement". This variable holds what would go in an IF statement, like so: if ($statement) Here are some possible values of $statement: 12 >= 15 13.2 >= 13.1 12.1 >= 10.3 18 >= 16.2 The problem is, no matter what $statement holds, the IF statement ALWAYS comes back true, even if it isn't. I even echo out the value of statement and it looks just fine, so I'm not sure why it isn't working properly. Here is the code: <?php //Make sure the IF statement looks okay (error check) echo "if ($statement)<br>"; //Process the IF statement. if ($statement) echo 'TRUE<p>'; else echo 'FALSE<p>'; ?> Here is the output for the above code, this code is in a foreach loop, so thats why there are multiple lines. if (12 >= 15) TRUE if (12 >= 15) TRUE if (12 >= 15.3) TRUE if (12 >= 15) TRUE if (12 >= 15.3) TRUE As you can see, it is coming out true even if it shouldn't. Any help would be greatly appreciated =D Thanks. Quote Link to comment Share on other sites More sharing options...
per1os Posted June 25, 2007 Share Posted June 25, 2007 http://www.phpfreaks.com/forums/index.php/topic,145271.html I would suggest reading that. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 25, 2007 Author Share Posted June 25, 2007 Thank you Frost, but I'm still a bit confused. I saw the functions you posted: <?php function condition_run($condition_str) { $and_portion = explode("&&", $condition_str); if (count($and_portion > 0)) { $condition_true = true; foreach ($and_portion as $and) { $rest = explode(" ", $and); if (!condition_test($rest[0], $rest[2], $rest[1])) { $condition_true = false; break; } } return $condition_true; } return false; } function condition_test($val1, $val2, $condition) { switch ($condition) { case '<': if ($val1 < $val2) { return true; } break; case '>': if ($val1 > $val2) { return true; } break; } return false; } ?> But I'm not really sure how to make this fit my code. Here are the variables in my code: <?php $f[$stat]; //This holds just a single number $condition; // This holds the condition AND a number together. //An example of what it could look like is this: >= 5 ?> I know I could use explode() on the $condition and separate the condition from the number...but I still wouldn't know how those functions could help me, as the condition_test() function doesn't check for conditions like ">=" or "<=". In the first function, I see that it assumes your working with "&&" as the condition...which is not what I want. Is there some sort of built in PHP function that compares numbers? I had no idea that it wouldn't be valid with an if statement. I tried the IF statements above in a different file, and the ones that should have returned false actually did return false, so are we sure that is the problem? Quote Link to comment Share on other sites More sharing options...
per1os Posted June 25, 2007 Share Posted June 25, 2007 It is simply an example, if you do not want to do complex conditions you modify the code, so you do not split at &&. <?php function condition_run($condition_str) { $condition_true = true; $rest = explode(" ", $condition_str); if (condition_test($rest[0], $rest[2], $rest[1])) { return true; } return false; } function condition_test($val1, $val2, $condition) { switch ($condition) { case '<': if ($val1 < $val2) { return true; } break; case '>': if ($val1 > $val2) { return true; } break; // add as many cases as you want. case '>=': if ($val1 >= $val2) { return true;} break; } return false; } ?> The basic gist of it is, take that code and manipulate it to suit your needs. It is just like a template. EDIT:: As I explained in the other post, $string = "1 > 2"; will return true as a string returned as an int > 0 which php generally takes any number in an if greater than zero as being true IE: <?php if ('this is just a simple string') { echo 'Wow this is true!'; } if ('12 < 3') { echo 'Wow this is also true!'; } ?> Since it is taking those literally like they are strings, it does not know the difference. You might be able to use www.php.net/eval , but that could be pretty dangerous. IE: <?php if (eval("<?php if ($statement) { return true;}else {return false;} ?>")) { echo 'Returned true!'; }else { echo 'Returned False!'; } ?> Unsure if it will work but yea. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 25, 2007 Author Share Posted June 25, 2007 Wooooo, I got the condition_test() function to work and do what I wanted with the script. Thank you very much Frost xD 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.