Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/167986-eval-in-if-condition-problem/
Share on other sites

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

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

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.

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...

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.