www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP in PHP?

  1. #1
    Join Date
    Jul 2004
    Location
    Irish lad lost in Atlanta
    Posts
    18

    PHP in PHP?

    Hi guys!

    I'm getting a massive headache trying to assign a large string of mixed HTML/PHP code to a member variable of an object in a PHP script. The variable represents content that will be displayed/executed as part of a DisplayPage fucntion I have coded.

    Is there a straightforward way of passing 'mini-PHP' scripts to classes in this way?

    Thanks!

  2. #2
    Join Date
    May 2003
    Location
    London, UK
    Posts
    327
    One way to approach this is using eval():

    PHP Code:
    <?php

    class Executor {

        var 
    $code_to_exec;

        function & 
    Executor()
        {
            
    //
        
    }

        function 
    setCode($string)
        {
            
    $this->code_to_exec $string;
        }

        function 
    exec()
        {
            eval(
    $this->code_to_exec);
        }
    }

    $ex =& new Executor();
    $ex->setCode('for ($i = 1; $i<10; $i++) { echo "<b>Hello</b> <i>World!</i>"; }');
    $ex->exec();

    $ex->setCode('echo "<br /><br />Goodbye Cruel world!";');
    $ex->exec();

    ?>

  3. #3
    Join Date
    Jul 2004
    Location
    Irish lad lost in Atlanta
    Posts
    18
    Thanks Shimon!
    I've solved the problem - I was going about it the wrong way.

    Instead of 'dipslaying' the PHP/HTML code I've executed it and passed the resulting HTML content to my class.

    Thanks for the Eval information. That looks like the way to go if I was truly executing PHP.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles