1

Let say I have a string stored in the $condition variable:

$condition = '2 == 2'; // returns TRUE 

or

$condition = 'my_own_function()'; // which also returns TRUE 

How can I convert them into a condition in an IF statement? Say:

if (some_magic_converter_function($condition)){ // execute something } 

it will be the same as if I execute

if (my_own_function()){ // execute something } 

Is there any other ways I can safely evaluate the string into a condition of an IF statement? How?

15
  • 5
    eval, but no, please don't. Commented Mar 30, 2017 at 10:29
  • 1
    Love your passive aggresive comments here and there @JonStirling ;) Commented Mar 30, 2017 at 10:32
  • 1
    @Timmetje I'm trying very hard not to use eval() at all cost :) Commented Mar 30, 2017 at 10:33
  • 1
    @FarizLuqman That is a good approach :P Commented Mar 30, 2017 at 10:34
  • 1
    Ok ok I'll answer, but pretty please don't use it ! :) Commented Mar 30, 2017 at 10:36

2 Answers 2

1

The basic rule of thumb: If you need to use eval(), you're doing something very very wrong.

But for entertainment purposes:

$condition = "return 2==2;"; $test = eval($condition); if ($test) { echo 'Oh Noes'; } function my_own_function() { return true; } $condition = 'return my_own_function();'; $test = eval($condition); if ($test) { echo "More Noes"; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Okay I won't use it sigh
1

I found out that we can fully make use of preg_match to accomplish the above task:

/** * Function parse_condition * @param string $condition * @return bool */ function parse_condition($condition){ // match {a} {condition} {b} preg_match('/.*(.*?)\s(.*?)\s(.*?)/sU', $condition, $condition_matches); // condition_matches[1] will be a, remove $, () and leading/tailing spaces $a = trim(str_replace(array('$','()'),'',$condition_matches[1])); // condition_matches[2] will be the operator $operator = $condition_matches[2]; // condition_matches[3] will be b, remove $, () and leading/tailing spaces $b = trim(str_replace(array('$','()'),'',$condition_matches[3])); // It is advisable to pass variables into array or a "hive" // but in this example, let's just use global // Make variable's variable $$a accessible global $$a; // And for $$b too global $$b; $cmp1 = isset($$a)?($$a):($a); $cmp2 = isset($$b)?($$b):($b); switch($operator){ case '==': return($cmp1 == $cmp2); break; case '!=': return($cmp1 != $cmp2); break; case '===': return($cmp1 === $cmp2); break; case '!==': return($cmp1 !== $cmp2); break; default: return false; break; } } 

so I can use it like this:

// TESTCASES $var_a = 100; $var_b = 100; $var_c = 110; $var_d = (double) 100; if(parse_condition('$var_a == $var_b')){ // TRUE echo 'Yeah I am Good!' . PHP_EOL; } if(parse_condition('$var_a != $var_c')){ // TRUE echo 'Good for this one too!' . PHP_EOL; } if(parse_condition('$var_b == $var_c')){ // FALSE echo 'Yeah I am Good!' . PHP_EOL; }else{ echo 'But I am not with this one :(' . PHP_EOL; } if(parse_condition('$var_a != 110')){ // TRUE echo 'I can compare values too' . PHP_EOL; } if(parse_condition('$var_a === $var_d')){ // FALSE // }else{ echo 'Or even tell the difference between data types' . PHP_EOL; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.