给我的函数访问外部变量 [英] Giving my function access to outside variable

查看:90
本文介绍了给我的函数访问外部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在外面有一个数组:

  $ myArr = array(); 

我想让我的函数可以访问外部数组,所以它可以为它添加值

 函数someFuntion(){
$ myVal = //在此处进行一些处理以确定$ myVal的值
$ myArr [] = $ myVal;
}

我该如何给函数的变量赋予正确的范围?

解决方案

默认情况下,当你在函数中时,你无法访问外部变量。



如果你希望你的函数可以访问一个外部变量,你必须声明它为 global ,函数:

 函数someFuntion(){
global $ myArr;
$ myVal = //在此处进行一些处理以确定$ myVal的值
$ myArr [] = $ myVal;
}

有关更多信息,请参阅 变量作用域



但请注意,使用全局变量不是一个好习惯:有了这个,你的函数不再是独立的。





更好的办法是让你的函数返回结果:
$ b

  function someFuntion() {
$ myArr = array(); //首先,你有一个空数组
$ myVal = //这里有一些处理来确定$ myVal
$ myArr [] = $ myVal的值; //将$ myVal放入数组
return $ myArr;
}

然后调用这个函数:

  $ result = someFunction(); 



您的函数也可以带参数,甚至

 函数someFuntion(array& $ myArr){
$ myVal = //在这里进行一些处理以确定$ myVal的值
$ myArr [] = $ myVal; //将该$ myVal放入数组
}

然后,像这样调用函数:

  $ myArr = array(...); 
someFunction($ myArr); //该函数将获得$ myArr并修改它

使用这个:




  • 您的函数接收外部数组作为参数

  • 可以修改它,因为它是通过引用传递的。 >
  • 最好的做法是使用全局变量:您的函数是一个单元,与任何外部代码无关。





有关更多信息,请阅读 Functions
部分,特别是以下小节:

>

I have an array outside:

$myArr = array();

I would like to give my function access to the array outside it so it can add values to it

function someFuntion(){
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;
}

How do I give the function the right scoping to the variable?

解决方案

By default, when you are inside a function, you do not have access to the outer variables.


If you want your function to have access to an outer variable, you have to declare it as global, inside the function :

function someFuntion(){
    global $myArr;
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;
}

For more informations, see Variable scope.

But note that using global variables is not a good practice : with this, your function is not independant anymore.


A better idea would be to make your function return the result :

function someFuntion(){
    $myArr = array();       // At first, you have an empty array
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;      // Put that $myVal into the array
    return $myArr;
}

And call the function like this :

$result = someFunction();


Your function could also take parameters, and even work on a parameter passed by reference :

function someFuntion(array & $myArr){
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;      // Put that $myVal into the array
}

Then, call the function like this :

$myArr = array( ... );
someFunction($myArr);  // The function will receive $myArr, and modify it

With this :

  • Your function received the external array as a parameter
  • And can modify it, as it's passed by reference.
  • And it's better practice than using a global variable : your function is a unit, independant of any external code.


For more informations about that, you should read the Functions section of the PHP manual, and,, especially, the following sub-sections :

这篇关于给我的函数访问外部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆