I'm writing a shopping cart in PHP, and part of its function is to calculate shipping for a given set of products. For users with Javascript enabled, I want to provide the smoothest experience possible by having an accurate shipping calculation done on the client side so that changes in the cart do not require page reloads or AJAX calls.
Currently I have a function in PHP that calculates the shipping charge, and another function in Javascript that mirrors its functionality. I'm concerned that the formula may change frequently, and having to rewrite two separate functions in two different languages (and make sure their outputs match) could create maintainability problems.
What I'm looking for is a way of representing the formula for calculating shipping charges in some language agnostic way, so that it can be interpreted by both PHP and Javascript. Currently the shipping formula is only a function of a single variable (number of items) although I would like the capability to add more without too much rewriting.
Is there an accepted way to represent a fairly simple mathematical formula in a way that can be interpreted by both PHP and Javascript? Preferably without writing my own interpreter?
Edit: I don't need a whole function, just a fairly simple mathematical formula along the lines of "4 + 1.5n". The kind of thing you would find in a typical spreadsheet cell.