在eval上使用str_replace更改变量[关闭]

i want to know how to change variables on eval

Look the code, please help me

$status = "HD";

$vari = "XX";

$condition = "if($status<>'99' && $status<>'XX' && $status<>'XM' && $status<> 'XD' && $status<> 'XY' && $status<> 'XE') 
{ echo 'YES'; } else { echo 'NO'; }";

$condition = eval($condition );

echo $condition;
echo "<hr>";

$resp = str_replace('status','vari',$condition);
echo eval($resp);

The problem is, not change the variables.. see if changes results YES or NO...

And i want to using this work on my WHILE Expression

while($condition) {

   ...

}

Thank You

Please correct me if i wrong, but from your code above, when you eval you $condition string, you put it back again into $condition. Unless your evaluated code have "return" in it, it will return null ( reff : http://php.net/manual/en/function.eval.php).

But your problem is actually miss-use of " on $condition. When you use " to wrap string(s) on php, it will change every variable on that string to the value defined for that variable above that code.

So, instead having your code evaluate by php, you're actually sending wrong code to eval().

your current $condition code is :

if(HD<>'99' && HD<>'XX' && HD<>'XM' && HD<> 'XD' && HD<> 'XY' && HD<> 'XE') { echo 'YES'; } else { echo 'NO'; }

and what you're trying to achive is :

if($status<>"99" && $status<>"XX" && $status<>"XM" && $status<> "XD" && $status<> "XY" && $status<> "XE") { echo "YES"; } else { echo "NO"; }

Please change " to ' since string(s) wrap in ' will be as it is (if you want to put ' inside it, please escaped it using backslash (\) ) and see what happen.

my work around :

$status = "HD";
$vari = "XX";
$condition = 'if($status<>"99" && $status<>"XX" && $status<>"XM" && $status<> "XD" && $status<> "XY" && $status<> "XE") { echo "YES"; } else { echo "NO"; }';
eval($condition );
echo $condition;
echo "<hr>";
$resp = str_replace('status','vari',$condition);
eval($resp);