1

I was wondering how can I assign a conditional assignment to a php variable and use it inside other conditional, like this:

$cndtnal='&& $x==4'

if($y==5 $cndtnal){
   print 'Hello World';
}

Thanks.

6
  • @MarcB As long as there's no user-provided input being passed into it, I don't think it's an issue. Commented Aug 29, 2012 at 3:42
  • Thanks, but how can I use the eval in this line: if($y==5 $cndtnal) like this? if($y==5 eval($cndtnal)) Commented Aug 29, 2012 at 3:44
  • 2
    It does make debugging more difficult. Commented Aug 29, 2012 at 3:44
  • if (eval('$y==5'.$cndtnal)). But it is evil. I urge you find another way to do it. Commented Aug 29, 2012 at 3:56
  • 1
    You do know that $x=4 will always evaluate to true and as such there's no reason to include it in your equality statement, right? Commented Aug 29, 2012 at 3:57

1 Answer 1

1

You should try avoiding the use of eval as much as possible, but if you want to use it, then you could do:

$cndtnal='&& $x=4';
if($y==5 .eval("return '$cndtnal';")  ){
   echo 'Hello World';
}
else {
    echo "this";
}
Sign up to request clarification or add additional context in comments.

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.