php 函数内部的函数。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1631535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:19:59  来源:igfitidea点击:

Function inside a function.?

phpfunction

提问by Posto

This code produces the result as 56.

此代码产生的结果为 56。

function x ($y) {
    function y ($z) {
        return ($z*2);
    }

    return($y+3);
}

$y = 4;
$y = x($y)*y($y);
echo $y;

Any idea what is going inside? I am confused.

知道里面发生了什么吗?我很迷惑。

回答by Duroth

X returns (value +3), while Y returns (value*2)

X 返回(值 +3),而 Y 返回(值*2)

Given a value of 4, this means (4+3) * (4*2) = 7 * 8 = 56.

给定值为 4,这意味着(4+3) * (4*2) = 7 * 8 = 56

Although functions are not limited in scope (which means that you can safely 'nest' function definitions), this particular example is prone to errors:

尽管函数的范围不受限制(这意味着您可以安全地“嵌套”函数定义),但这个特定示例容易出错:

1) You can't call y()before calling x(), because function y()won't actually be defined until x()has executed once.

1)你不能在调用 y()之前调用x(),因为函数在执行一次y()之前不会被定义x()

2) Calling x()twice will cause PHP to redeclare function y(), leading to a fatal error:

2)调用x()两次会导致PHP重新声明函数y(),导致致命错误:

Fatal error: Cannot redeclare y()

致命错误:无法重新声明 y()

The solution to both would be to split the code, so that both functions are declared independent of each other:

两者的解决方案是拆分代码,以便两个函数都声明为相互独立:

function x ($y) 
{
  return($y+3);
}

function y ($z)
{
  return ($z*2);
}

This is also a lot more readable.

这也更具可读性。

回答by Luká? Lalinsky

(4+3)*(4*2) == 56

Note that PHP doesn't really support "nested functions", as in defined only in the scope of the parent function. All functions are defined globally. See the docs.

请注意,PHP 并不真正支持“嵌套函数”,因为它仅在父函数的范围内定义。所有函数都是全局定义的。请参阅文档

回答by Anti Veeranna

Not sure what the author of that code wanted to achieve. Definining a function inside another function does NOT mean that the inner function is only visible inside the outer function. After calling x() the first time, the y() function will be in global scope as well.

不确定该代码的作者想要实现什么。在另一个函数内部定义一个函数并不意味着内部函数只在外部函数内部可见。第一次调用 x() 后, y() 函数也将在全局范围内。

回答by d.raev

This is useful concept for recursion without static properties , reference etc:

对于没有静态属性、引用等的递归来说,这是一个有用的概念:

function getReqursiveItems($id){
    $allItems = array();

    function getItemms($parent_id){
       return DB::findAll()->where('`parent_id` = $parent_id');
    } 

    foreach(getItems($id) as $item){
         $allItems = array_merge($allItems, getItems($item->id) );
    }

    return $allItems;
}

回答by Nanhe Kumar

It is possible to define a function from inside another function. the inner function does not exist until the outer function gets executed.

可以从另一个函数内部定义一个函数。内部函数在外部函数被执行之前不存在。

echo function_exists("y") ? 'y is defined\n' : 'y is not defined \n';
$x=x(2);
echo function_exists("y") ? 'y is defined\n' : 'y is not defined \n';

Output

输出

y is not defined

y 未定义

y is defined

y 已定义

Simple thing you can not call function y before executed x

简单的事情你不能在执行 x 之前调用函数 y

回答by Mike Valstar

Your query is doing 7 * 8

您的查询是 7 * 8

x(4) = 4+3 = 7and y(4) = 4*2 = 8

x(4) = 4+3 = 7y(4) = 4*2 = 8

what happens is when function x is called it createsfunction y, it does not run it.

发生的情况是,当函数 x 被调用时,它会创建函数 y,但它不会运行它。