代码之家  ›  专栏  ›  技术社区  ›  Reza Saadati

带两个圆括号的PHP函数:calc(1)(2)

  •  2
  • Reza Saadati  · 技术社区  · 6 年前

    JavaScript语言 :

    function calc(x) {
      return function(y) {
         return x + y;
      };
    }
    
    console.log(calc(1)(2));

    这会回来的 3 .

    我也试过了 菲律宾比索 :

    function calc($x) {
        return function($y) { 
            return $x + $y; 
        };
    }
    
    echo calc(1)(2);
    

    2 . 我收到一封电子邮件通知:

    为什么变量 x 未定义?是因为PHP无法使用,还是我做错了什么?

    1 回复  |  直到 6 年前
        1
  •  3
  •   asynts    6 年前

    http://php.net/manual/en/class.closure.php

    http://php.net/manual/en/functions.anonymous.php

    function calc($x) {
        return function($y) use($x){ 
            return $x + $y; 
        };
    }