I would like to execute a string as if it were PHP code.
An example would be:
$string='round(24.6,2)';
I would like to convert $string to executable syntax. Is there a way to do this?
eval()
is the function you want. But: eval() is evil!
You can use eval('round(24.6,2)'), but this is usually frowned upon for multiple reasons. Why do you want to do this?
Related
This may sound strange, but here goes.
I like using this technique of building a string in php
printf(__('This is %1$s, this is %2$s'), myFunction1(), myFunction2());
Obviously this directly prints the results whenever the function is called, but I would like to use this technique to just build a string, and then use it later elsewhere.
Is this possible?
Thanks guys.
Use sprintf to do this:
$var = sprintf(__('This is %1$s, this is %2$s'), myFunction1(), myFunction2());
How to output functions that are stored in mysql database? When I try it the output looks like this:
<-- function();-->
What have you stored in your table:
function names? or complete functions?
if function names you can do:
$functionname()
if you have complete functions you probably could eval() it.
Please be very very careful with eval().
Why do you store functions in your mysql table? It just looks weird to me.
And perhaps there is a better solution.
Use the eval() function.
It evaluates a string as PHP code
Say I have a variable containing PHP code, can I include its content as if it was a normal PHP file ?
For example, the PHP code could be a class declaration.
You don't have a variable containing php code. You have a string.
You can execute a string as php with the evil eval function, but puppies AND kittens will die!
eval($your_variable);
Be aware about security holes!This is very dangerous and should NOT be based on user's input !
You could use eval to evaluate any code that you have in your string, however it is evil. What exactly are you trying to do?
I find myself in the situation where I need to call a PHP function stored as a string from another PHP function.
<?php
$phpcodestring = "<?php echo 'Hello World!' ?>";
echo $phpcodestring;
?>
How can I get Hello World to render to screen with the above structure?
Back up a few steps - why do you need to do that?
99.9% of the time there is a better way than eval().
Or you could use create_function() which internally uses something like eval() so isn't much better, especially when you are not defining the function body directly with a string literal so you can be sure there and then there is nothing potentially dangerous.
You can use the infamous eval() if you thoroughly trust the source of the string. How the hell did you find yourself in that situation?
Sounds like a case of eval().
eval("echo 'Hello world!';");
You should always consider whether it's a good idea or not, though. Security considerations of eval() can be daunting.
G
You can use the eval() function: http://php.net/manual/en/function.eval.php
eval($phpcodestring);
An alternative approach would be to use create_function method, which is a poor implementation of lambda-style functions in PHP.
I am interested in knowing the ways we can call/run Perl scripts in PHP.
You can use a simple Php Perl extension within the Php code. This will allow you to execute code and Perl variables,functions and instantiate objects.
$perl = new Perl();
$perl->require("test1.pl");
You can make use of
system
exec
backtick operator
Just like any other executable.
But avoid it, it's very inefficient. What do you want to achieve by doing it?
Use something like
system("/your/kewl/script.pl");