How to use string in IF condition in PHP

user3686600

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use if condition in string concatenation

how to use string variable as filter condition in dplyr

How to use a string entered in a txt file as an if condition

how to use if condition with string inside the condition using c#

PHP - How can regex replace a string by condition

How to use if ..else condition to select folder in PHP

How to use MySqli SELECT with OR condition correct in PHP?

how to use php condition in html option tag

Evaluate string as condition PHP

How to get the ajax response value/string and use it on the if condition?

How to use Shiny inputs in a string for reactive function and as a condition for observe event

How to use ansible when condition when string contains '@'

how to use WHERE condition to find specific value from string in table?

How to use String operation like split and compare in Drools condition

how can i use string interpolation based on condition in angular

How to store a condition as a string in node property or relationship property and use it in a query?

How to use condition operator pulled from database and added to script with PHP

Use a string as a condition in If- else

Oracle use a string inside IN condition

how to use PHP to convert numerical string to float?

how to use a string as formula for mathematical calculations in php?

How to use php array+string in html?

When i use if else condition in query based on condition, how can i get that value on view in PHP CODEIGNITER

How to use SpinButton in a condition

how to use #ifdef with an OR condition?

How to use condition with routerLink?

How to use an if condition in a query?

How to use if condition for location

How use order by condition