0

I have array I have converted the array into string using implode function like below i need the $str variable as condition in if statement like below

[0]=>"1==1" 
[1]=>"1==1" 
[2]=>"2==1"
$str = "1==1 && 1==1 && 2==1";
if(1==1 && 1==1 && 2==1)
user3686600
  • 121
  • 6

3 Answers3

0

You can put the string into php eval() function.

$str = "1==1 && 1==1 && 2==1";
$question = eval("return $str;");

if ($question) {

} else {

}

I hope it will be usefull for you.

Grommy
  • 367
  • 1
  • 8
0

Though it is not a good way to do such activity, you can use eval() for this:

<?php

$str = "1==1 && 1==1 && 2==1";

$condition = eval("return $str;");

if($condition) {
    echo "True";
} else {
    echo "False";
}

Demo for False Situation

Demo for True Situation

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

Try this code , i think you required this condition.

        $array[] = "1==1";
        $array[] = "1==1";
        $array[] = "2==1";

        $str = implode(" && ", $array);

        $condition = eval("return $str;");

        if($condition)
            return "true code";
        else
            return "false code";
dev87
  • 154
  • 1
  • 9