SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create_function problem

    PHP Code:
    $function='create_function("$a","return $a;");';
    $newfunct=eval($function);
    echo 
    $newfunct("DISPLAY ME"); 
    Returns:

    Fatal error: Function name must be a string in c:\program files\wamp\www\phpd\phpd.php on line 32

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     $function='create_function(\'$a\',\'return $a;\');';
     
    $newfunct=eval($function);
     echo 
    $newfunct('DISPLAY ME'); 
    Try that. And it'll be $function that's the function, not $newfunct. (Why are you using eval?)

  3. #3
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    PHP Code:
     $function='create_function(\'$a\',\'return $a;\');';
    $newfunct=eval($function);
    echo 
    $newfunct('DISPLAY ME'); 
    Try that. And it'll be $function that's the function, not $newfunct.
    Returns the same error.

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you even using eval?

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, what line is 32? If it's the function-call one, then that's because you need to actually catch the output to create_function.

  6. #6
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using eval because I want to generate the function on the runtime, and because it gives an unique name of the each newly created function.

  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the entire code? It'll make it a lot easier.

  8. #8
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I managed it working like this:

    PHP Code:
    $newfunc=create_function('$a,$b''return $a;'); 
    But within eval:

    PHP Code:
    eval("$newfunc=create_function('$a,$b', 'return $a;');"); 
    Returns:

    Parse error: syntax error, unexpected '=', expecting '}' in c:\program files\wamp\www\phpd\phpd.php on line 32

  9. #9
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And so I ask once again -- why are you using eval? It's completely unnecessary.

  10. #10
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because I want to use custom function within another php code,the function may be called twice, and I don't want it to cause redeclare error. Then there will be just two functions with different names and identical bodies.

  11. #11
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, what? You can't get redeclaration errors on create_function. On top of that, eval won't change that at all. Why don't you just declare the function ABOVE the block of code, so you don't have anything duplicated.

  12. #12
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for your quick response.

    But if I needed it declared before the body, I would do it without the eval(). I'm just wondering why the eval(), returns such a message.

    I forgot to mention that if it is declared with function fn_name(){} in eval, it works fine.

    Looks like = makes the problem.

  13. #13
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason it doesn't work in eval is because double quotes parses the variable out of it, which results in "=create...". There's no variable. Add a backslash in front of the dollar sign (or use single quotes) to fix it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •