Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Location
    North Canton, Ohio
    Posts
    118
    Thanks
    11
    Thanked 4 Times in 4 Posts

    echoing php code

    Let's say I have the following:
    $content="<?php echo 'Hello world\n'; ?>"

    and I wanted the php code to run what's inside that variable. How would I be able to do that?


    The reason for this is, I'm working on a CMS and if the content in the database contains php, I want it to be ran.

    -Thanks in advanced

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    The closest thing I could find was using eval():
    http://us3.php.net/eval

    Scroll down to the entry dated: 31-Jan-2006

    That looks similar to what you're doing.

  • Users who have thanked mlseim for this post:

    Blaher (02-17-2008)

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Location
    North Canton, Ohio
    Posts
    118
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Thanks that works. It won't work of course if I have html code in it (<?php code ?> some html), but I can easily manipulate the string to only eval the php code.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    The eval() allows closing and opening ?> <?php tags. You can put inline html if you want.

    If I remember correctly it assumes you are initially in php "mode", so you must put an initial closing ?> tag if the string starts with inline html.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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