Execute PHP code in a string
Needless to say you should find another solution ASAP. In the meantime you can eval the code like this:
$str = '<h1>Welcome</h1><?php echo $motto?><br/>'; // Your DB content
eval("?> $str <?php ");
Demo: http://codepad.org/ao2PPHN7
I can't stress that enough: eval is dangerous, and application code shouldn't be in the database. Try a template parser like Smarty, Dwoo, or my favorite: Twig.
How to execute string php code to run
As already stated you can use eval($string);
. Beware that eval()
evaluates a string as PHP code, so you need to be careful with the strings you allow to be passed!
If you take the string yourself from a predefined set of strings you created (using a swtich
statement or something similar) it shouldn't be a problem.
If the string comes from the outside (user input or cURL requests or whatever) you shouldn't trust it and you should avoid using this method.
execute php code within string
never mind...bone-headed brain asleep. Just needed parenthesis...I had only tried curvy braces:
$CurrentMembershipYear = '2016';
echo '<h2>Two-Year Membership for '.$CurrentMembershipYear.' and '.(intval($CurrentMembershipYear)+1).'</h2>';
String variable to execute PHP code
You can use eval() for this
$test = <<<END
<p> <?php
echo time();
?> </p>
END;
ob_start();
eval("?>$test");
$result = ob_get_clean();
Execute a string of PHP code on the command line
Yep, it's there in PHP 5.2's cli SAPI.
If you cannot upgrade and that's the case that there's no such option in PHP 5.2 (I don't have it at hand to test), you can do this:
echo "<?php echo \"hi\\n\";" | php
hi
Original:
There is indeed a -r
option (though I'm not sure about PHP 5.2):
php -r "echo 'hi';";
hi
Just make sure you are using the command line version of PHP. php --version
should give you something like this (notice "cli"):
php --version
PHP 5.3.0 (cli) (built: May 20 2010 19:05:12) (DEBUG)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
Run PHP and HTML code from a string
You don't want to do this. If you still insist, you should check eval aka evil
function. First thing to know, you must not pass opening <?php
tag in string, second you need to use single quotes for php script You want to evaluate (variables in single quotes are not evaluated), so your script should look something like:
<?php
$string = '
<?php $var = "John"; ?>
<p>My name is <?php echo $var; ?></p>
';
// Replace first opening <?php in string
$string = preg_replace('/<\?php/', '', $string, 1);
eval($string);
However, this is considered very high security risk.
How to execute PHP code stored as a string?
eval()
is the function you want. But: eval() is evil!
Related Topics
Get Facebook Meta Tags with PHP
Cakephp Does Not Use My Models
PHP Keep Checkbox Checked After Submitting Form
Parametrized Pdo Query and 'Limit' Clause - Not Working
Reordering Checkout Fields in Woocommerce 3
Html,PHP - Escape '<' and '>' Symbols While Echoing
How to Access a Deep Object Property Named as a Variable (Dot Notation) in PHP
Is There a Good PHP Geolocation Service
PHP Simplexml Namespace Problem
How to Get Insert Id in Mssql in PHP
Pg_Query Result Contains Strings Instead of Integer, Numeric
Wordpress Prepared Statement with In() Condition
What Factors Make PHP Unicode-Incompatible