How to put a PHP code in a string?

Discussion in 'PHP' started by marktopper, Sep 14, 2011.

  1. #1
    Is it possible to put a PHP code in a string.


    see the strings functions here:
    http://php.net/manual/en/function.eval.php


    I want to make a string called "$name" which should have the value as the following:
    $db = new mysql("localhost", "user", "Pass", "DB");
    $query = $db->query("SELECT * FROM strings'") or die($db->error());
    $page_HTML = mysql_fetch_array($query);eval($page_HTML['name']);
    PHP:

    Would that be possible somehow?


    EDIT:
    I tried the following:
    $name = ' $db = new mysql("localhost", "user", "Pass", "DB");
    $query = $db->query("SELECT * FROM strings'") or die($db->error());
    $page_HTML = mysql_fetch_array($query);eval($page_HTML['name']); ';
    PHP:
     
    marktopper, Sep 14, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    Confused ?
    
    function do_this(){
    $db = new mysql("localhost", "user", "Pass", "DB");
    $query = $db->query("SELECT * FROM strings'") or die($db->error());
    $page_HTML = mysql_fetch_array($query);eval($page_HTML['name']);
    }
    
    //execute the function
    
    do_this();
    
    
    PHP:
    do_this() is a function, a command to execute what's between { here & here } so if you look at it this way, do_this is effectively a variable that holds a string of commands.
    Other then that could you explain a little more about the purpose of your request, I'm sure someone will reply.


    EDIT: I'm confusing myself now, but using your reference to eval, something like this should work:

    
    <?php
    
    function do_this(){
    $db = new mysql("localhost", "user", "Pass", "DB");
    $query = $db->query("SELECT * FROM strings'") or die($db->error());
    $page_HTML = mysql_fetch_array($query);eval($page_HTML['name']);
    }
    
    ?>
    
    <html>
        <head>
        <!-- ... -->
        </head>
        <body>
            <!-- ... -->
            <?php eval("?>" . do_this() . "<?"); ?>
            <!-- ... -->
        </body>
    </html>
    
    PHP:
     
    Last edited: Sep 15, 2011
    MyVodaFone, Sep 15, 2011 IP
  3. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #3
    JohnnySchultz, Sep 15, 2011 IP