简体   繁体   English

在PHP中定义一个全局变量

[英]define a global variable in PHP

I want to use a variable outside my foreach. 我想在我的foreach之外使用一个变量。 how can I solve that in PHP. 如何在PHP中解决该问题。 How can I use $ID outside the foreach? 如何在foreach之外使用$ ID?

foreach($row as $key => $value) {
  $ID = $value['Id'];
}

Your $ID variable can already be used outside of your foreach : a control structure doesn't come with its own variable scope . 您的$ID变量已经可以在您的foreach之外使用:控制结构没有它自己的变量作用域


As a quick illustration, the following portion of code : 作为快速说明,下面的代码部分:

$ID = 0;

$array = array(1, 2, 3);
foreach ($array as $value) {
    $ID = $value;
}

var_dump($ID);

Will get you : 会让你:

int 3

Which shows that the $I D variable that's used inside the foreach loop is the same as the one that exists outside that loop. 这表明在foreach循环内使用的$I D变量与该循环外存在的变量相同。



Edit after the comments : 在评论后编辑:

Not initializing the variable before the loop, you'd get exactly the same result : the variable would be created the first time it used -- and it would in the same scope as in the example I posted. 如果没有在循环之前初始化变量,您将得到完全相同的结果:该变量将在首次使用时创建-并且其作用域与我发布的示例相同。


Another example, now, using a function inside the foreach loop (only works with PHP >= 5.3) : 现在foreach一个例子,在foreach循环中使用一个函数(仅适用于PHP> = 5.3)

$array = array(1, 2, 3);
foreach ($array as $value) {
    $func = function () use ($value) {
        $ID = $value;
        var_dump($ID);
    };
    $func();
}

var_dump($ID);

There, the $ID variable is not initialized before the loop -- it's created in the function that's inside the loop. 在那里, $ID变量不会在循环之前初始化-它是在循环内部的函数中创建的。

This example would get the following output : 此示例将获得以下输出:

int 1
int 2
int 3

( ! ) Notice: Undefined variable: ID ... on line 15

null

Basically : 基本上:

  • You get three correct output, from inside the function 您从函数内部获得了三个正确的输出
    • Those show the $ID variable, inside the anonymous function, is getting a value 这些显示匿名函数内部的$ID变量正在获取值
  • But, after the loop, you get a notice : the $ID variable doesn't exist. 但是,在循环之后,您会注意到: $ID变量不存在。
    • Which show that the $ID variable created inside the function is not the same as the $ID variable outside the function : the function comes with its own variable scope. 这表明, $ID在函数内部创建的变量是不一样的$ID的功能外变量:功能带有自己的变量范围。

You can use it outside your foreach , variables scopes are not limited by statement structure. 您可以在foreach之外使用它,变量范围不受语句结构的限制。

On the other hand, they are limited by functions scope. 另一方面,它们受功能范围的限制。

See this page from the manual http://php.net/manual/en/language.variables.scope.php for more informations about variables scope. 请参阅手册http://php.net/manual/en/language.variables.scope.php上的此页面,以获取有关变量范围的更多信息。

$ID = null;
foreach($row as $key => $value) {
  $ID = $value['Id'];
}
//do something with $ID...

The reason for this is that if the for loop is never gone through (that is, $row was an empty array) $ID would never be set. 这样做的原因是,如果永远不会通过for循环(即$ row是一个空数组),就不会设置$ ID。 Therefore, $ID has to be set beforehand or else it won't be in scope at all and if you try to access $ID 's value, you will hit errors. 因此,必须预先设置$ID ,否则它将根本不在范围内,如果尝试访问$ID的值,则会遇到错误。

Having a variable inside a foreach loop does affect it's scope. 在foreach循环中包含变量确实会影响其范围。 You are free to manipulate that variable before, after or in the foreach loop to your desires. 您可以在所需的foreach循环之前,之后或之中随意操作该变量。

there is not much sense in assigning all values to single variable in turn. 将所有值依次分配给单个变量没有多大意义。
if you want only last one, it can be substituted with 如果只想要最后一个,可以用

$value = array_pop($row);
$ID = $value['Id'];

but I doubt it's what you really need. 但我怀疑那是您真正需要的。 what is whole task you're trying to solve? 您要解决的整个任务是什么? May be you want an array? 可能需要数组吗?

$id_arr = array();
foreach($row as $key => $value) {
  $id_arr[] = $value['Id'];
}

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

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