Johnain Posted October 10, 2009 Share Posted October 10, 2009 Hi All I have never used eval() before to generate and execute php code. The code that I am trying to generate is a varying number of rows that should say (and execute) ... $incomingvariable = $_POST['incomingvariable'] ; ... where the incoming variable will be 1_saleprice to (for example ) 10_saleprice when $pricecount (see below) has a value of 10 The variable $pricecount has a value of 8 with the data I am using and it IS passed correctly (I use EngInSite debugger so I know that). I am using a for loop. The variable that holds the number of incoming records is called $pricecount. It reads like this ... 93 for ($z = 1; $z <= $pricecount; $z += 1) 94 { 95 $y = trim($z) ; 96 97 $varval = "$" . $y . "_saleprice = $" . "_POST['" . $y . "_saleprice'] ;" ; 98 99 eval("\$varval = \"$varval\";"); 100 } You will see that I have put the terminating executing code semi-colon into the variable $varval in this example and I still get an error with or without it. (I split the $ and the _POST because otherwise it reads it directly as $_POST[') The error WITH the semi colon is ... Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/q07peri/public_html/admin/mdprodcodeedit.php(99) : eval()'d code on line 1 The error WITHOUT the closing semi-colon is exactly the same. Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/ Share on other sites More sharing options...
Daniel0 Posted October 10, 2009 Share Posted October 10, 2009 Try to echo instead of eval()'ing. In that way it'll be easier to see what the error is. Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/#findComment-934180 Share on other sites More sharing options...
Johnain Posted October 10, 2009 Author Share Posted October 10, 2009 Try to echo instead of eval()'ing. In that way it'll be easier to see what the error is. I'll do that. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/#findComment-934182 Share on other sites More sharing options...
Johnain Posted October 10, 2009 Author Share Posted October 10, 2009 Try to echo instead of eval()'ing. In that way it'll be easier to see what the error is. Ok, echo comes up with $varval = "$1_saleprice = $_POST['1_saleprice']"; If I was writing the line of code I would write ... $1_saleprice = $_POST['1_saleprice'] ; So it looks about right to me. It looks as though I have not grasped eval() properly. Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/#findComment-934183 Share on other sites More sharing options...
Daniel0 Posted October 10, 2009 Share Posted October 10, 2009 Ok so then just do eval($varval);. eval() simply takes a piece of PHP code and evaluates it ("executes it") in the current context. Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/#findComment-934184 Share on other sites More sharing options...
Johnain Posted October 10, 2009 Author Share Posted October 10, 2009 Ok so then just do eval($varval);. eval() simply takes a piece of PHP code and evaluates it ("executes it") in the current context. Ok, I replaced my original eval with eval($varval) ; But I got Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/q07peri/public_html/admin/mdprodcodeedit.php(102) : eval()'d code on line 1 I made no changes to $varval itself Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/#findComment-934187 Share on other sites More sharing options...
PFMaBiSmAd Posted October 10, 2009 Share Posted October 10, 2009 A) There is absolutely no reason to be using eval() for what you are doing (all you are doing is using a variable as part of the index name of an array. You are NOT dynamically producing php code that would require using eval().) B) Variables cannot start with a number, so you will need to change your concept anyway, C) If you post an example of what your form looks like, someone could directly show you how to simplify what you doing. Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/#findComment-934295 Share on other sites More sharing options...
Johnain Posted October 11, 2009 Author Share Posted October 11, 2009 A) There is absolutely no reason to be using eval() for what you are doing (all you are doing is using a variable as part of the index name of an array. You are NOT dynamically producing php code that would require using eval().) B) Variables cannot start with a number, so you will need to change your concept anyway, C) If you post an example of what your form looks like, someone could directly show you how to simplify what you doing. Thanks for that. In the end I came to much the same conclusion and what I now do is simply load up one array with the values that I want and another array with the IDs that I want so that when (much later in the script) I want to use them in an SQL update I simply lookup the two arrays. In fact it's an excellent example of using experience of what's easy in one language and trying to apply it in another without thinking it through properly. So I need to be more careful next time. But a big word of thanks to those who helped me. Quote Link to comment https://forums.phpfreaks.com/topic/177174-solved-syntax-error-using-eval/#findComment-934798 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.