Jump to content

Archived

This topic is now archived and is closed to further replies.

dwees

Loose function definitions?

Recommended Posts

Hi there,

I've written some Javascript that uses the new Canvas element to produce images in HTML, which is great, but the images aren't permanent, and I'd like to make them so.  Also, Canvas isn't supported in all browsers, and seems lack text capability and anti-aliasing to create sharp pictures.

So I'd like to start from scratch and recode the whole thing in PHP, and I thought a good idea would be to basically convert as much of the code as I can directly from the Javascript, then work around the details, and use $_POST to send the data from the client side back to the server, then return an image depending on what information the server-side script receives, and even better, using PHP I could cache the images so that if an identical call is made by the user, I can just immediately return the image.  All of this seems like it won't be an issue for me, however...

In the Javascript I have a pair of nested functions defined like so:

[code]
function f(x, fu) {
var myfunction = new Function("x", "return "+ fu + ";")
return myfunction (x);
}
[/code]

where x is a number, and fu is a string.  When I do this, I can construct the required function from the user input, and then use that constructed function to modify the number x.  Basically the myfunction is not strictly defined, and the definition can be adjusted at runtime.

Is this type of definition possibly in PHP?  I took a look at my one skinny PHP book I have, and I didn't see any examples of how to do this, so before I go and buy a more expensive fat PHP book, I thought I'd ask around and see if anyone can help me with this.

Thanks in advance,

Dave

Share this post


Link to post
Share on other sites
Thanks for the super fast response, Eval looks like what I want to use. 

Dave

Share this post


Link to post
Share on other sites
You should also look at the function [url=http://www.php.net/create_function]create_function()[/url].  This may be closer to your original idea.

Ken

Share this post


Link to post
Share on other sites
This create_function thing has virtually identical syntax to what I was using in Javascript, thanks!

I'm pretty sure I can work it around either way, but option #2 is closer to what I was doing in Javascript.  Actually, if you include  a var $newfunc = etc... it's identical syntax.

Dave

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.