How can I present a formula in a way that is understood by both Javascript and PHP?

I am writing a shopping cart in PHP, and part of its function is to calculate delivery for a given set of products. For users with Javascript enabled, I want to provide the most seamless experience by performing accurate client-side delivery calculations so that changes in the cart do not require page reloads or AJAX calls.

I currently have a function in PHP that calculates shipping costs and another function in Javascript that reflects its functionality. I am concerned that the formula may change frequently, and rewriting two separate functions in two different languages ​​(and making sure their output matches) may create maintainability problems.

What I'm looking for is a way to present a formula for calculating shipping charges in any language agnostically, so this can be interpreted by both PHP and Javascript. Currently, the delivery formula is a function of only one variable (number of elements), although I would like to add more without too much rewriting.

Is there an acceptable way to represent a fairly simple mathematical formula in a way that can be interpreted by both PHP and Javascript? It is advisable not to write your own interpreter?

Edit: I don't need an entire function, just a simple mathematical formula in the lines "4 + 1,5n". What you find in a regular spreadsheet.

+4
source share
5 answers

Generally, your best bet is simply to support two versions of the code. The most elegant solution, of course, is to write the server logic in JavaScript itself (see cappuccino.org ); that would be a bad choice for you because it looks like you already wrote a ton of PHP code.

However, if you really feel the need to scratch this itch, consider calling AJAX to the server. Although a little slower, most users will not notice.

+4
source

I would say do it on the server side, and the user is AJAX, it will not matter much to the user, but saving two versions can matter when the user sees different calculations after sending the order.

In any case, if for some reason you do not want AJAX at all, the best way would be to have a single javascript library that runs both on the client side and the server side. You can execute javascript with php on the server side. for example you can use the following from linuxto execute javascript

http://www.mozilla.org/js/spidermonkey/ http://www.mozilla.org/rhino/ http://www.wxjavascript.net/

see http://en.wikipedia.org/wiki/Server-side_JavaScript for more options

+3
source

This is a bad idea and will become a worse idea, the more complicated the calculation.

Speaking of which, Javascript really supports the dollar sign as legal in variable names. There is a fairly general syntax between the two languages, in which you can easily write code that analyzes and works the same in each case, as long as you are dealing only with simple mathematics. Arrays can be a little trickier. Check out PHP.JS , a set of libraries to simulate the built-in PHP functions in Javascript.

(Edit: I edited the link to php.js, not knowing that someone else would post the same during my editing. Credit / horror for him .;))

+1
source

May be. Take a look at php.js. It provides you all the php functions, but in JavaScript.

You can also write all validation in php and use Ajax for client-side request.

+1
source

We can say that it is time to add some automated tests to your application and run them at least every time you want to deploy your application to your production server - and if some of them do not work, cancel the deployment.

You will have tests for both PHP and JS code, and some of these tests will calculate some total amounts / costs / shipping costs; and if something goes wrong due to some code change, you can detect the problem automatically, without your application disrupting the production process.

Of course, this requires a little more work (both for writing tests, setting up the building platform, and for maintaining test data); but it will be of great value ...


Another possible solution would be to write your calculation code only in Javascript and run it both on the client side (this is not difficult, obviously), and on the server side.

From PHP, you can execute JS code using the Spidermonkey PECL extension (note that it is still in beta, and you need to be able to install PHP extensions, which are likely to be possible only if you are the administrator of your server and are not sure of stability).

Here's an article about it: Using JavaScript in PHP with PECL and SpiderMonkey .

With this solution, you can use JS for code that only works on the client; PHP for code that runs only on the server ... And JS for code that works on both sides.

+1
source

All Articles