How to compile php code at runtime?

Discussion in 'PHP' started by bhargava, Nov 28, 2007.

  1. #1
    Hi, What I mean by the question is if i have some thing like

    
    $script = "<? echo \"\nHello World!\"; ?>"
    // I Want to compile the above string here or just include it here 
    
    PHP:

     
    bhargava, Nov 28, 2007 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    hmm, can you be more specific? but basing on your sample it seemed that you just want to run a php line, then the above code will work, assuming you have php setup with your webserver(usually apache)
     
    serialCoder, Nov 28, 2007 IP
  3. bhargava

    bhargava Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What I want to do now is to include the code that '$script' has into a function .... what i am trying to do is a template substitution , that is there are some variables that come at runtime to a function and that function has a predefined string. Function just has to substitute the arguments (array with keys and values) and return whatever was evaluated by that 'script' string .. am I clear??
     
    bhargava, Nov 28, 2007 IP
  4. Kwaku

    Kwaku Well-Known Member

    Messages:
    1,217
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #4
    function doSomething($func) {
    echo "Hello ";
    eval($func);
    }

    $func = "echo 'World!';";
    doSometing($func);

    like that?
     
    Kwaku, Nov 28, 2007 IP
  5. bhargava

    bhargava Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    a more elaborate example:
    
    
    function subst ($list) {
     $script = "$feed = \"\"; 
                      foreach ($list as $user) {
                           $feed +="\nHello $user!\";
                      }";
     //include the $script here and use $list to substitute
     //one simple thing i can do is to write $script to some file and include that file
     //i will get some output to $feed  and that is what i want  
    }
    
    PHP:
     
    bhargava, Nov 28, 2007 IP
  6. bhargava

    bhargava Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    exactly!! Thanx Mr. Kwaku!! I was a fool not to think of 'eval', because it is availble in python and i was using python too ...
     
    bhargava, Nov 28, 2007 IP