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
Related
As it is deprecated, I will need to change my all code which contain mysql_result. Since this may take long time, I am looking for a simplest possible option.
Normally I would use this:
$varname=mysql_result($result_query,0,"settingName");
now I found a function which may be a replacement:
$varname=mysqli_fetch_row($result_query)[0];
But in this case, I am unable to see which setting I am getting from database. Also, when I change an order in database, I will need to update all numbers.
How can use a function which I can use a similar syntax?
Try:
$varname = mysqli_fetch_assoc($result_query)['settingName'];
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?
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());
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'm trying to use webspace provided by my university. They are currently using an outdated version of php, 5.1 I think. Anyway it doesn't have a json_encode function, however, I need a json_encode function (or equivalent) for my code to work.
So if anyone could explain to me the syntax for the return of the json_encode function or point me to a website that explains it nicely it'd be much appreciated.
Thanks
Don't reinvent the wheel: upgrade.php library.
http://pear.php.net/package/Services_JSON
Check here. It tells you to use require 'jsonwrapper.php'; at the start of your code in versions of PHP where json_encode is not available.