Jump to content

[SOLVED] Syntax error using eval()


Johnain

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.