ctrLogicDotNet Posted July 29, 2009 Share Posted July 29, 2009 The thing I am trying to acheive: foreach($array as $k=>$v){ foreach($arrayEreg as $ereg){ $bool = (ereg($ereg,$k)!==false)?'true':'false'; if(eval($bool)){ unset($array[$k]); } else{ continue; } } } The error I got: Parse error: syntax error, unexpected $end in {whatever path of the file}(21) : eval()'d code on line 1 I've read the following post : http://www.phpfreaks.com/forums/index.php/topic,127492.0.html But still add no positive results. Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/ Share on other sites More sharing options...
Mark Baker Posted July 29, 2009 Share Posted July 29, 2009 You're setting $bool to a string value of either 'true' or 'false'. Neither value is a valid PHP statement that can be evaluated... And I don't see any need to use eval with your sample code anyway. foreach($array as $k=>$v){ foreach($arrayEreg as $ereg){ $bool = (ereg($ereg,$k)!==false)?true:false; if($bool){ unset($array[$k]); } else{ continue; } } } should work Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886059 Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2009 Share Posted July 29, 2009 The test can be used directly - if(ereg($ereg,$k)!==false){ Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886064 Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 ...and from there, there's no reason why you can't just do this: foreach($array as $k=>$v){ foreach($arrayEreg as $ereg){ if (ereg($ereg,$k)!==false) unset($array[$k]); } } Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886068 Share on other sites More sharing options...
ctrLogicDotNet Posted July 29, 2009 Author Share Posted July 29, 2009 ok sorry for the misleading, my example is not really showing why I want to use an eval condition statement... I don't want to repeat the same code for 2 different cases, which only the codition differs... $cond; switch($function_value){ case 0: $cond = ''; case 1: $cond = '!'; } foreach($array as $k=>$v){ foreach($arrayEreg as $ereg){ $bool = (ereg($ereg,$k)!==false)?true:false; if(eval($cond.$bool)){ unset($array[$k]); } } } P.S.: And by the way which BBCode do you guys use to PHP Highlight the Code Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886079 Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2009 Share Posted July 29, 2009 You still should not be using eval() to perform logic. Write out the logic that you actually need (put an if/else inside of the inner foreach() loop.) And the answer to the error was already given by Mark Baker. The parameter given to eval() must be valid php code. true/false/!true/!false is not php code. eval() invokes a separate instance of the php language engine each time it is called. Using eval() like this is going to take 100-1000 times longer than if logic is used, which for a large set of values is going to create a performance problem. Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886088 Share on other sites More sharing options...
ctrLogicDotNet Posted July 29, 2009 Author Share Posted July 29, 2009 Thank You all for your replies, I'm new to this forum and I would like to say that the good response time in here makes it a place I will continue to look in for answers or to answer to some issues I've resolved on my own. Didn't exactly know of how much the performance could be affected by the use of eval. Also thanks to point out that its use in my case is not a good idea, even though it would have kept me of repeating the same code twice or more, just in case I would like to make a modification to it for all possible cases. Guess I could still rely on a private function which has the code I don't want to repeat, is this the best alternative?, any suggestions? Anyway for what I'm concern, this question is solved in my case and I'll have a better look at what is valid PHP and what is not. I still can do something like: $bool = true; if(!$bool){ ...whatever... } without PHP complaining about anything, why?. maybe: ... if(eval($cond.'$bool')) ... would have been the valid PHP code that I was looking for, now that I think of it... Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886103 Share on other sites More sharing options...
ctrLogicDotNet Posted July 29, 2009 Author Share Posted July 29, 2009 maybe: ... if(eval($cond.'$bool')) ... would have been the valid PHP code that I was looking for, now that I think of it... no it's not :-\ Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886233 Share on other sites More sharing options...
Mark Baker Posted July 29, 2009 Share Posted July 29, 2009 I still can do something like: $bool = true; if(!$bool){ ...whatever... } without PHP complaining about anything, why?. Because it's perfectly valid code, even if the proximity of the lines makes it rather meaningless. You're setting a variable $bool to be a boolean with a value of True, then testing the value of $bool using an if statement, and executing a block of code if the value of that boolean is false. The compiler doesn't care that the lines are adjacent, just that they are syntactically correct. Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886276 Share on other sites More sharing options...
KevinM1 Posted July 29, 2009 Share Posted July 29, 2009 Putting the code in a function, with the condition of whatever it is you need to keep track of passed in as an argument, seems like the most logical solution to me. Quote Link to comment https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/#findComment-886294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.