简体   繁体   English

'function'和'use'以及'array_filter'如何在PHP中运行?

[英]How does 'function' and 'use' and 'array_filter' work in PHP?

I am familiar with creating a PHP function placed at the top of the .php file such as: 我熟悉在.php文件顶部创建一个PHP函数,例如:

function my_little_function($parm1,$parm2) {
   if ($parms < $parm2) {
   return "yes";
   } else {
   return "no";
   }
}

Then call it like this: 然后像这样称呼它:

$result = my_little_function("1","2");
echo "The answer is $result." . "\n";

I have some code, I didn't write it, which uses "function" and "use" together inside of a traditional use of a function like my_little_function above. 我有一些代码,我没有写它,它使用“函数”和“使用”一起在传统使用上面的函数my_little_function。

I'm puzzled by this and have some questions for you more experienced PHP developers. 我对此感到困惑,并为您更有经验的PHP开发人员提出一些问题。 Here is part of the working PHP code I'm referring to: 以下是我所指的工作PHP代码的一部分:

$neededObject = array_filter($st_ny_trip->STOPS->STOP,function($e) use ($final_desired_dest,$connect_raw){return $e->NAME == $final_desired_dest && DateTime::createFromFormat("m/d/Y g:i:s a", $e->TIME) > $connect_raw;});

$e is not set in any part of the function or the rest of the program, so what is using $e ? $e未在函数的任何部分或程序的其余部分中设置,那么使用$e什么? How does it get passed a value and how is it being used? 它是如何传递一个值的,它是如何被使用的? There appears to be no name for this function, so I don't know how it is being called, how is that being done? 似乎没有这个功能的名称,所以我不知道它是如何被调用的,这是怎么做的?

Is this creating a function, on-the-fly to be used and it gets re-generated each time this code gets called? 这是否会创建一个即时使用的函数,并且每次调用此代码时都会重新生成它? If it's a function, why not create it outside of this function and call it? 如果它是一个函数,为什么不在这个函数之外创建它并调用它?

I've also not used 'use' myself yet, so that's unfamiliar to me. 我自己也没有使用'使用',所以这对我来说并不熟悉。 I looked it up on php.net and it just looks like a way to assign a value to something, but I couldn't find any practical examples to demonstrate why it's needed and when it should be used. 我在php.net上查了一下,它看起来像是一种为某些东西分配值的方法,但我找不到任何实际的例子来证明它为什么需要它以及什么时候应该使用它。

I looked up array_filter and it says it's "Filters elements of an array using a callback function". 我查找了array_filter ,它说它是“使用回调函数过滤数组的元素”。 I don't know what a call back function is. 我不知道回叫功能是什么。 Is it referring to function($e) ? 它是指function($e)

Should the above line of PHP code for $neededObject be formatted differently so it is easier to read? 上面的$neededObject的PHP代码是否应该以不同的格式进行格式化,以便更容易阅读?

Let's use array_map() to explain what's going on. 让我们使用array_map()来解释发生了什么。

We want to duplicate the input of an array: so if the input is aa , the output would be aaaa . 我们想要复制数组的输入:所以如果输入是aa ,输出将是aaaa

So the normal way, would be to create a function and then pass it to array_map() : 所以通常的方法是创建一个函数,然后将它传递给array_map()

$array = range('a', 'e');

$new_array = array_map('duplicate', $array);
print_r($new_array);

function duplicate($string){
    return $string.$string;
}

Online demo 在线演示

But what if you want to use this function only once ? 但是如果你只想使用这个功能一次呢? Since PHP 5.3 , there is something called anonymous functions , we use it like the following: PHP 5.3开始 ,有一种称为匿名函数的东西,我们使用它如下:

$array = range('a', 'e');

$new_array = array_map(function($string){
    return $string.$string;
}, $array);
print_r($new_array);

Online demo 在线演示

Now, let's say for example you want to add a standard value from another variable. 现在,让我们假设您想要从另一个变量添加标准值。 That's easy with global variables. 全局变量很容易。 But as we know, global variables are evil and should be avoided. 但正如我们所知,全球变量是邪恶的,应该避免。 We may use use() : 我们可以使用use()

$array = range('a', 'e');
$standard_value = ',';

$new_array = array_map(function($string)use($standard_value){
        // $standard_value becomes available inside the function
    return $string.$standard_value.$string;
}, $array);
print_r($new_array);

Online demo 在线演示

use() can be become also useful if we use a reference to write to an external variable while looping: 如果我们在循环时使用引用写入外部变量, use()也会变得有用:

$array = range('a', 'e');
$another_string = '';

$new_array = array_map(function($string)use(&$another_string){// note &
    $another_string .= $string.$string; // overwrite $another_string
    return $string.$string;
}, $array);
print_r($new_array);
echo PHP_EOL . $another_string;

Online demo 在线演示

the $e variable acts as a normal function parameter, and will thus be passed by the code calling the function, see the documentation for the value of $e when using array_filter . $e变量充当普通函数参数,因此将由调用函数的代码传递,请参阅使用array_filter$e的值的文档。

The use statement imports variables from the local scope into the anonymous' function 's scope. use语句将变量从本地范围导入匿名'函数的范围。

$myvar = 'world';
$myFunc = function ($test) use ($myvar) {
    return $test . ' ' . $myvar;
};
echo $myFunc('hello'); // echoes 'hello world';

If you did not include the use ($myvar) part, then isset($myvar) would return false from inside the anonymous function, since it has a separate scope. 如果你没有包含use ($myvar)部分,那么isset($myvar)将从匿名函数内部返回false,因为它有一个单独的作用域。

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

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