简体   繁体   English

将简单的函数从jS转换为PHP

[英]Converting a simple function from jS to PHP

I have a simple function hash function in JS that I'm struggeling with converting into PHP (for server side validation), it is a simple math function, and I'm not a PHP expert, could any of you shed some light into why I can't have ($variable)(function) in PHP next to each other? 我在JS中有一个简单的函数哈希函数,正在努力转换成PHP(用于服务器端验证),这是一个简单的数学函数,而且我不是PHP专家,你们中的任何一个都可以弄清楚为什么我不能在PHP中并排放置($ variable)(function)吗?

Here is the function in jS: 这是jS中的函数:

function claculate(a,b){
    var g=function(a){
      return Math.round(a/Math.pow(10,Math.round(Math.LOG10E*Math.log(a)))*10)
    };
    var f=function(a){
        var h=function(b){
            return b(a)
        };
        var j=function(b){
            return g(a)
        }; 
        return a<b?j:h
     };
     var o=((f(a)(g)%2)||(f(a*b)(g)%7*1/7)||11)^f(b)(g);
     return another_function(a,o);  
}

And here is my version in PHP, but there is an error on the $o line, and I'm not sure why ... 这是我的PHP版本,但是$ o行上有错误,我不确定为什么...

function claculate($a, $b){
   $g = function ($r){
       return round($r / pow(10, round(M_LOG10E * log($r))) * 10);
   };
   $f = function ($a) use (&$a, &$b, &$g) {
       $h = function ($b) use (&$b, &$a) {
           return $b($a);
        };
        $j = function ($b) use (&$b, &$g, &$a) {
           return $g($a);
        };
        return ($a < $b) ? $j : $h;
    };
    // $o throws error
    $o = $f($a)($g) % 2 || $f($a * $b)($g) % 7 * 1 / 7 || 11 ^ $f($b)($g);
    return another_function($a, $o);
}

Any thoughts or right direction is more than welcomed! 任何想法或正确的方向都受到欢迎! Thanks 谢谢

In general, this works the same: 通常,这是相同的:

function g($r)
{
  return round($r / pow(10, round(M_LOG10E * log($r))) * 10);
}

function claculate($a, $b)
{
  $o = ((g($a) % 2) || (g($a * $b) % 7 * 1 / 7) || 11) ^ g($b);
}

The first problem is in difference when evaluating OR statement. 第一个问题是在评估OR语句时的区别。 (JS returns number, PHP bool.) But this can be solved by using of some conditions. (JS返回数字,PHP布尔值。)但这可以通过使用某些条件来解决。

The worst is that JS uses 2 more digits for real numbers. 最糟糕的是JS使用2个以上的数字作为实数。

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

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