eval() function 
Author Message
 eval() function

Hi,
I suspect this is a very simple question, but I can't seem to get the
following code to work.  I am trying to process a list of checkboxes.
Since they are dynamically generated I've had to give them dynamically
generated names on the form. In the code I am using to process the form
I have the following:

 for($cnt = 0; $cnt < $num_items; $cnt++ )
{
      if (  !(empty  (eval("return $item'.$cnt.';") ) )  )  //var names
are $item1, $item2 ..
     {
           echo "Item ".$cnt." has been checked.";
      }
 }

This produces the following error (the if statement is line 46)

Parse error: parse error, unexpected T_EVAL, expecting T_VARIABLE or '$'
in p:\test_apache\admin_proc_assign_reviews.php on line 46

If anyone can see the problem I would greatly appreciate your help.

Also, is there a PHP FAQ somewhere, if so I suspect this type of
question might already be on it?

Thanks,

Craig Dillabaugh



Tue, 26 Apr 2005 00:17:40 GMT  
 eval() function

Quote:

>  for($cnt = 0; $cnt < $num_items; $cnt++ )
> {
>       if (  !(empty  (eval("return $item'.$cnt.';") ) )  )  //var names
...
> If anyone can see the problem I would greatly appreciate your help.

eval() does not return a string, so your empty() call is broken. See the
docs at www.php.net/eval for an explanation of eval()'s return values (they
differ between php3 and php4).

Quote:
> Also, is there a PHP FAQ somewhere, if so I suspect this type of
> question might already be on it?

www.php.net

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Tue, 26 Apr 2005 00:44:59 GMT  
 eval() function

Quote:

> Hi,
> I suspect this is a very simple question, but I can't seem to get the
> following code to work.  I am trying to process a list of checkboxes.
> Since they are dynamically generated I've had to give them dynamically
> generated names on the form. In the code I am using to process the form
> I have the following:

>  for($cnt = 0; $cnt < $num_items; $cnt++ )
> {
>       if (  !(empty  (eval("return $item'.$cnt.';") ) )  )  //var

return $item'.$cnt.';
is your problem, you have ' marks in invalid places.

try something along the lines of:
$varname=$item.$cnt;
if(isset($$varname) && !empty($$varname))

Quote:
> names
> are $item1, $item2 ..
>      {
>            echo "Item ".$cnt." has been checked.";
>       }
>  }

> This produces the following error (the if statement is line 46)

> Parse error: parse error, unexpected T_EVAL, expecting T_VARIABLE or '$'
> in p:\test_apache\admin_proc_assign_reviews.php on line 46

> If anyone can see the problem I would greatly appreciate your help.

> Also, is there a PHP FAQ somewhere, if so I suspect this type of
> question might already be on it?

did you try http://www.php.net/ or http://www.php.net/manual/en/faq.php yet?

--

PHP POSTERS: Please use comp.lang.php for PHP related questions,
              alt.php* groups are not recommended.



Tue, 26 Apr 2005 00:46:09 GMT  
 eval() function
Thanks Justin and Stephan,

Justin's solution worked for me.  I had to make on minor modification in case
anyone cares:

$varname = "item".$cnt;

and that did the trick.

Thanks for your help.

Craig

Quote:


> > Hi,
> > I suspect this is a very simple question, but I can't seem to get the
> > following code to work.  I am trying to process a list of checkboxes.
> > Since they are dynamically generated I've had to give them dynamically
> > generated names on the form. In the code I am using to process the form
> > I have the following:

> >  for($cnt = 0; $cnt < $num_items; $cnt++ )
> > {
> >       if (  !(empty  (eval("return $item'.$cnt.';") ) )  )  //var

> return $item'.$cnt.';
> is your problem, you have ' marks in invalid places.

> try something along the lines of:
> $varname=$item.$cnt;
> if(isset($$varname) && !empty($$varname))

> > names
> > are $item1, $item2 ..
> >      {
> >            echo "Item ".$cnt." has been checked.";
> >       }
> >  }

> > This produces the following error (the if statement is line 46)

> > Parse error: parse error, unexpected T_EVAL, expecting T_VARIABLE or '$'
> > in p:\test_apache\admin_proc_assign_reviews.php on line 46

> > If anyone can see the problem I would greatly appreciate your help.

> > Also, is there a PHP FAQ somewhere, if so I suspect this type of
> > question might already be on it?

> did you try http://www.php.net/ or http://www.php.net/manual/en/faq.php yet?

> --

> PHP POSTERS: Please use comp.lang.php for PHP related questions,
>               alt.php* groups are not recommended.



Tue, 26 Apr 2005 01:00:43 GMT  
 eval() function

Quote:

> Thanks Justin and Stephan,

> Justin's solution worked for me.  I had to make on minor modification
> in case
> anyone cares:

> $varname = "item".$cnt;

> and that did the trick.

> Thanks for your help.

> Craig



> >>Hi,
> >>I suspect this is a very simple question, but I can't seem to get the
> >>following code to work.  I am trying to process a list of checkboxes.
> >>Since they are dynamically generated I've had to give them dynamically
> >>generated names on the form. In the code I am using to process the form
> >>I have the following:

> >> for($cnt = 0; $cnt < $num_items; $cnt++ )
> >>{
> >>      if (  !(empty  (eval("return $item'.$cnt.';") ) )  )  //var

> >return $item'.$cnt.';
> >is your problem, you have ' marks in invalid places.

> >try something along the lines of:
> >$varname=$item.$cnt;
> >if(isset($$varname) && !empty($$varname))

> >>names
> >>are $item1, $item2 ..
> >>     {
> >>           echo "Item ".$cnt." has been checked.";
> >>      }
> >> }

> >>This produces the following error (the if statement is line 46)

> >>Parse error: parse error, unexpected T_EVAL, expecting T_VARIABLE or '$'
> >>in p:\test_apache\admin_proc_assign_reviews.php on line 46

> >>If anyone can see the problem I would greatly appreciate your help.

> >>Also, is there a PHP FAQ somewhere, if so I suspect this type of
> >>question might already be on it?

> >did you try http://www.php.net/ or
> http://www.php.net/manual/en/faq.php yet?

Oh, now I see what you are doing... you could have used
eval("return 'item'.$cnt.;") as well.

However, it would be easier to setup the HTML form to use arrays to
pass. For instance:

<input type="checkbox" name="item[0]" value="1">
<input type="checkbox" name="item[1]" value="1">
<input type="checkbox" name="item[2]" value="1">
<input type="checkbox" name="item[3]" value="1">
<input type="checkbox" name="item[4]" value="1">
<input type="checkbox" name="item[5]" value="1">

Then use something like:

foreach($_POST['item'] as $item_number=>$value){
     if(!empty($value))
         echo 'item #'.$item_number.' has been checked.<br>'."\n";

Quote:
}

This is the way that I usually handle it.

--

PHP POSTERS: Please use comp.lang.php for PHP related questions,
              alt.php* groups are not recommended.



Tue, 26 Apr 2005 02:25:46 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. EVAL function

2. Why is there no eval function in scheme?

3. eval function

4. Function eval in Haskell?

5. Defining new global functions without eval.

6. eval to invoke a local function?

7. eval to call function

8. "eval" function

9. Call to undefined function eval

10. Adding functions at runtime - understanding eval

11. Adding functions at runtime - understand eval

12. Dynamic creation of generic functions without eval?

 

 
Powered by phpBB® Forum Software