0

I've defined this function in php

function ifstatement($statement = 'if(10 > 9;1+1;2+2)') {
// $statement = str_replace(' ', '', $statement);

if (strpos($statement, 'if(') > -1) {
   $statement = rtrim(ltrim(str_replace(' ', '', $statement), 'if('), ')');
   $exp = explode(';', $statement);
  if ( $exp[0] ) {
    if (strpos($exp[1], 'if(' )> -1) {
     return ifstatement($exp[1]);
    } else {
     return 1;
   }
  } else {
  if (strpos($exp[2], 'if(')> -1) {
    return ifstatement($exp[2]);
  } else {
   return 0;
  }
 }
}else{
  echo 'out';
  }
}

the problem is the function always return 1 even if the condition in the if statement in the function argument is false which is tested with

if($exp[0])

it looks like the $exp[0] comes as a string, how can i convent this to be tested as if argument

4
  • 2
    What on Earth are you trying to accomplish here? Commented Sep 16, 2011 at 12:11
  • am trying to create function that allow users to use if statement to compare two value specified(i.e 10 and 9 for this case) in their form, $statement is the string am expecting from database.. Commented Sep 16, 2011 at 12:25
  • 1
    Whatever you are trying to do here, you approach is all wrong. I just tried to re-write this in a way that makes sense to me and I can't, because it just doesn't. Please outline exactly what you are trying to achieve, and maybe we can give you some pointers. Commented Sep 16, 2011 at 12:28
  • I've a form with multiple field including the text input where the user specifies the logic statement(IF() statement), the statement is stored in the db and during viewing information some calculation has to be done basing of the logic statement specified..the string $statement is what stored in database..hope its clear now.. Commented Sep 16, 2011 at 13:06

3 Answers 3

2

$exp[0] contains 10 > 9 (whatever you have before first ;), so it will evaluate to false only it it's an empty string or '0'. To properly evaluate this you should use the eval() function:

$condition = '10 > 9';
$result = eval('return ' . $condition . ';');

Note: don't ever use eval() in production environment unless you are really sure that your input is harmless and you can't do it another way.

Sign up to request clarification or add additional context in comments.

9 Comments

eval? That if is not a PHP if even. And please do not that easily suggest to use eval. Seriously, eval is not for production use.
You are right, it's dangerous, But in this case only eval() will work, unless he writes a lot of code for proper parsing of conditions or narrows down possible expressions. (anyway, added warning to my answer)
PHP is not able to eval thw following code: IF(cond;true;false), even if you prefer eval over writing a parser on your own, using eval is not a solution here.
what could be the solution @harke, am trying to exract the condition from $statement so that i can use it in if statement.. Is there another way to do this?
But $result = eval('return cond;') will work. In this case it's eval or some custom lexer+parser.
|
0
$exp[0] = (boolean) $exp[0];

is probably the thing you're looking for

Comments

0

Unless you're going to do this properly (with a parser), you're better off making users specify their conditions in multiple fields. One row would have a text field for x, a select field for comparison operator (==,<,>), and another text field for y. Then they can specify whether to match all checks or any checks. When they're broken out like this you'll be able to execute the intended logic without having to implement a new language using strpos and eval, which is total madness.

You can see examples of this type of interface in Thunderbird's message filter editing:

enter image description here

and in iTune's smart playlist editing:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.