Executing PHP in a variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brett

    Executing PHP in a variable?

    I'm doing a file include as

    <?php $content = file_get_conten ts(TEMPLATEPATH . '/somefile.php');
    echo $content; ?>

    In somefile.php, there is PHP code. How can I execute the code once
    $content is displayed as above?

    Thanks,
    Brett

  • Andy Hassall

    #2
    Re: Executing PHP in a variable?

    On 11 Nov 2006 12:00:17 -0800, "brett" <account@cygen. comwrote:
    >I'm doing a file include as
    >
    ><?php $content = file_get_conten ts(TEMPLATEPATH . '/somefile.php');
    >echo $content; ?>
    >
    >In somefile.php, there is PHP code. How can I execute the code once
    >$content is displayed as above?


    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • larry@portcommodore.com

      #3
      Re: Executing PHP in a variable?


      brett wrote:
      I'm doing a file include as
      >
      <?php $content = file_get_conten ts(TEMPLATEPATH . '/somefile.php');
      echo $content; ?>
      >
      In somefile.php, there is PHP code. How can I execute the code once
      $content is displayed as above?
      >
      Thanks,
      Brett
      You want to use eval(), here is the entry on the on-line PHP manual (a
      great reference for PHP coding):



      Larry

      Comment

      • brett

        #4
        Re: Executing PHP in a variable?



        Your link doesn't seem to work.

        Comment

        • Andy Hassall

          #5
          Re: Executing PHP in a variable?

          On 11 Nov 2006 12:28:03 -0800, "brett" <account@cygen. comwrote:
          >
          >Your link doesn't seem to work.
          Works fine from here. In what way does it not seem to work?

          --
          Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
          http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

          Comment

          • brett

            #6
            Re: Executing PHP in a variable?

            You want to use eval(), here is the entry on the on-line PHP manual (a
            great reference for PHP coding):
            I still can't get this to work. There are three files involved. The
            chain starts here:

            LinkGuide.php --
            <a href="www.abc.c om/page1">Some Link</a(abc)

            somefile.php --
            Here's the link from LinkGuide:
            <?php $content = file_get_conten ts(TEMPLATEPATH . '/LinkGuide.php') ;
            echo $content; }
            ?>

            header.php --
            Finally, here's the two files from above:
            <?php
            $content = file_get_conten ts(TEMPLATEPATH . '/somefile.php');
            echo eval("$content" );
            ?>

            I get this error:
            Parse error: syntax error, unexpected T_STRING in
            d:\home\blog\wp-content\themes\ default\header. php(111) : eval()'d code
            on line 1

            Any suggestions?

            Thanks again,
            Brett

            Comment

            • Pedro Graca

              #7
              Re: Executing PHP in a variable?

              brett wrote:
              <?php
              $content = file_get_conten ts(TEMPLATEPATH . '/somefile.php');
              echo eval("$content" );
              echo eval('?>' . $content);
              ?>
              I like to think of it as if eval() evaluates its parameter /starting/ in
              PHP mode. So, if your data (from somefile.php) starts with "<?php" you
              need to get out of PHP mode first, then, when eval() sees "<?php" it
              will enter PHP mode and execute the rest.


              <?php
              unset($x);
              $data = '$x = 42;';
              eval($data);
              echo $x, "\n";

              unset($x);
              $data = '<?php $x = 42; ?>';
              eval('?>' . $data);
              echo $x, "\n";
              ?>

              The more balanced "eval('?>' . $data . '<?php');" gives a stupid parse
              error!

              --
              I (almost) never check the dodgeit address.
              If you *really* need to mail me, use the address in the Reply-To
              header with a message in *plain* *text* *without* *attachments*.

              Comment

              • brett

                #8
                Re: Executing PHP in a variable?

                I like to think of it as if eval() evaluates its parameter /starting/ in
                PHP mode. So, if your data (from somefile.php) starts with "<?php" you
                need to get out of PHP mode first, then, when eval() sees "<?php" it
                will enter PHP mode and execute the rest.
                The PHP is mixed with regular content. The file actually looks like
                this:

                somefile.php --
                Here's the link from LinkGuide:
                <?php $content = file_get_conten ts(TEMPLATEPATH . '/LinkGuide.php') ;
                echo $content; }
                ?>
                Then more text here.

                How can I get out of PHP mode when its mixed this way?

                Thanks,
                Brett

                Comment

                • Rudi Menter

                  #9
                  Re: Executing PHP in a variable?

                  brett:
                  How can I get out of PHP mode when its mixed this way?
                  You /are/ out immediately after the " ?" qualifier... no "mix"...

                  Rgds
                  --

                  Comment

                  • puzz

                    #10
                    Re: Executing PHP in a variable?

                    brett wrote:
                    I'm doing a file include as
                    >
                    <?php $content = file_get_conten ts(TEMPLATEPATH . '/somefile.php');
                    echo $content; ?>
                    >
                    In somefile.php, there is PHP code. How can I execute the code once
                    $content is displayed as above?
                    Why don't you just:

                    include( TEMPLATEPATH . '/somefile.php' );

                    --

                    Comment

                    • Peter Fox

                      #11
                      Re: Executing PHP in a variable?

                      Following on from puzz's message. . .
                      >
                      >Why don't you just:
                      >
                      >include( TEMPLATEPATH . '/somefile.php' );
                      Is the Right Answer - (At last)

                      BTW the OP might also want to search the manual for "include path".
                      Either run time or php.ini setting options.

                      --
                      PETER FOX Not the same since the pancake business flopped
                      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
                      2 Tees Close, Witham, Essex.
                      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

                      Comment

                      Working...