简体   繁体   English

如何以Javascript和PHP理解的方式表示公式?

[英]How can I represent a formula in a way that is understood by both Javascript and PHP?

I'm writing a shopping cart in PHP, and part of its function is to calculate shipping for a given set of products. 我正在用PHP编写购物车,其部分功能是计算给定产品组的运费。 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. 对于启用了Javascript的用户,我希望通过在客户端进行准确的运费计算来提供最流畅的体验,以便购物车中的更改不需要页面重新加载或AJAX调用。

Currently I have a function in PHP that calculates the shipping charge, and another function in Javascript that mirrors its functionality. 目前我在PHP中有一个计算运费的函数,以及Javascript中反映其功能的另一个函数。 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. 我正在寻找的是一种表示以某种语言无关的方式计算运费的公式,以便它可以被PHP和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? 是否有一种可以用PHP和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". 编辑:我不需要一个完整的函数,只是一个相当简单的数学公式,沿着“4 + 1.5n”。 The kind of thing you would find in a typical spreadsheet cell. 您可以在典型的电子表格单元格中找到的那种东西。

Generally, you are best off just to maintain two versions of the code. 通常,您最好只维护两个版本的代码。 The most elegant solution, of course, is to write the server logic in JavaScript itself (see cappuccino.org ); 当然,最优雅的解决方案是用JavaScript本身编写服务器逻辑(参见cappuccino.org ); this would be a poor choice for you because it sounds like you have already written a ton of PHP code. 这对你来说是一个糟糕的选择,因为听起来你已经编写了大量的PHP代码。

However, if you really feel the need to scratch this itch, consider an AJAX callback to the server. 但是,如果您真的觉得需要抓住这个痒,请考虑对服务器进行AJAX回调。 While slightly slower, most users will not notice. 虽然稍慢,但大多数用户都不会注意到。

I would say do it on server side, and user AJAX, It won't make much of difference for user, but maintaining two versions can make a difference when user after submitting the order sees the different calculation. 我会说在服务器端和用户AJAX这样做,它对用户没有太大的区别,但维护两个版本可以在用户提交订单后看到不同的计算有所不同。

Anyway if you due to some reason do not want AJAX at all, best way would be to have single javascript library which is executed both at client and server side. 无论如何,如果你由于某种原因根本不想要AJAX,那么最好的方法就是拥有在客户端和服务器端执行的单个javascript库。 You may execute javascript from php on server side. 你可以在服务器端从php执行javascript。 eg you may use following from linuxto execute javascript 例如,你可以使用linux中的以下执行javascript

http://www.mozilla.org/js/spidermonkey/ http://www.mozilla.org/rhino/ http://www.wxjavascript.net/ 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 有关更多选项,请参阅http://en.wikipedia.org/wiki/Server-side_JavaScript

This is a bad idea, and will become a worse idea the more complex the calculation becomes. 这是一个坏主意,而且计算变得越复杂就越糟糕。

That being said, Javascript does support the dollar sign as legal in variable names. 话虽如此,Javascript确实支持美元符号在变量名称中是合法的。 There's enough common syntax between the two languages that you could easily write code that parses and works the same in each, as long as you're only dealing with simple math. 这两种语言之间有足够的通用语法,只要您只处理简单的数学运算,就可以轻松编写解析并在每种语言中运行相同的代码。 Arrays may be a bit tricker. 数组可能有点琐碎。 Check out PHP.JS , a set of libraries to mimick PHP builtin functions in Javascript. 查看PHP.JS ,这是一组用于在Javascript中模仿PHP内置函数的库。

(Edit: I edited in the link to php.js without knowing someone else was going to post the same thing during my edit. Credit / horrors to him. ;) ) (编辑:我在php.js的链接中编辑,但不知道其他人会在我的编辑期间发布相同的内容。对他有信用/恐怖。;))

Maybe. 也许。 Take a look at php.js . 看看php.js。 It gives you all the php functions but in JavaScript. 它为您提供所有PHP功能,但在JavaScript中。

You could also write the whole validation in php, and use Ajax to query on the client side. 您也可以在php中编写整个验证,并使用Ajax在客户端进行查询。

One would say this is the time to add some automated tests to your application, and run them at least each time you want to deploy your application to your production server -- and if some of those fail, cancel the deployment. 可以说现在是时候向应用程序添加一些自动化测试,并且至少每次要将应用程序部署到生产服务器时运行它们 - 如果其中一些失败,则取消部署。

You'd have tests both for PHP and JS code, and some of those tests would calculate some total amount / charges / shipping costs ; 你有PHP和JS代码的测试,其中一些测试会计算一些总金额/费用/运费; and if something goes wrong because of some code change, you'd be able to detect the problem automatically, without your application breaking in production. 如果由于某些代码更改而出现问题,您将能够自动检测问题,而不会使您的应用程序在生产中中断。

Of course, it requires a bit more work (both to write the tests, configure the building platform, and maintain the test data) ; 当然,它需要更多的工作(编写测试,配置构建平台和维护测试数据) ; but that would be a great security... 但这将是一个伟大的安全......


Another possible solution would be to write your calculation code only in Javascript, and run it both on the client side (this is not the hard part, obviously) , and on the server side. 另一种可能的解决方案是仅在Javascript中编写计算代码,并在客户端运行它(这显然不是困难的部分) ,也可以在服务器端运行。

From PHP, you can execute JS code using the Spidermonkey PECL extension (Note it's still in beta, though ; and you'll need to be able to install PHP extensions, which will probably be possible only if you are admin of your server -- and not sure about stability) . 从PHP开始,您可以使用Spidermonkey PECL扩展来执行JS代码(注意它仍然处于测试阶段;并且您需要能够安装PHP扩展,这可能只有在您是服务器的管理员时才可能 -并且不确定稳定性)

Here is an article about it : Using JavaScript in PHP with PECL and SpiderMonkey . 这是一篇关于它的文章: 在PHP中使用JavaScript与PECL和SpiderMonkey

With this solution, you could use JS for the code that only runs on the client ; 使用此解决方案,您可以将JS用于仅在客户端上运行的代码; PHP for the code that only run on the server... And JS for the code that runs on both sides. PHP代码只在服务器上运行...而JS代表双方运行的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM