douzao9845 2017-04-23 12:07 采纳率: 0%
浏览 55

在我的情况下,访问函数内部数据的有效方法是什么?

I have a php page on which I get some data like this

$data = $_GET["data"];

$data is used on the page and inside a function i.e.

function myfunction() {
   some code here
}

this function is called many times on this page if needed

my question is what is efficient way to access the data inside the function?

1.

function myfunction() {
   $data = $_GET["data"];
}

or 2.

function myfunction() {
   global $data;
}
  • 写回答

1条回答 默认 最新

  • drny60365 2017-04-23 12:20
    关注

    most efficient way it to use $_GET["data"] directly where needed without declaring and assigning it to a new variable

    e.g

    //do your isset() checks here, not within the function
    
    function myfunction(){
        if($_GET['data']==="whatever"){ //note that === checks is faster than ==, if you are sure of the type being checked
            //do something
        }
    }
    
    评论

报告相同问题?