Jump to content

Loose function definitions?


dwees

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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.