Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的公共变量_Php_Public Method - Fatal编程技术网

php中的公共变量

php中的公共变量,php,public-method,Php,Public Method,我有这个功能: function map(){ $address = mashhad; // Google HQ $prepAddr = str_replace(' ','+',$address); $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');

我有这个功能:

function map(){          
        $address = mashhad; // Google HQ
        $prepAddr = str_replace(' ','+',$address);
        $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
        $output= json_decode($geocode);
        $latitude = $output->results[0]->geometry->location->lat;
        $longitude = $output->results[0]->geometry->location->lng;
        $ogt=owghat($month , $day , $longitude , $latitude  , 0 , 1 , 0);
}
function map() {
    //one method of defining globals in a function
    $GLOBALS['ogt'] = owghat($moth, $day, $longitude, $latitude, 0, 1, 0);
    // OR
    //the other way of defining a global in a function
    global $ogt;
    $ogt = owghat($month, $day, $longitude, $latitude, 0, 1, 0);
}
function map() {
    $ogt = owghat($month, $day, $longitude, $latitude, 0, 1, 0);
    return $ogt;
}

$ogt = map(); //defined in global scope.

我需要在另一个函数中使用$ogt表单,是否可能将ogt声明为公共变量?如果可能,我如何才能做到这一点?

您可以将$ogt变量声明为全局变量

资料来源:

1:


2:

您可以在函数中将其设置为全局:

function map(){          
        $address = mashhad; // Google HQ
        $prepAddr = str_replace(' ','+',$address);
        $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
        $output= json_decode($geocode);
        $latitude = $output->results[0]->geometry->location->lat;
        $longitude = $output->results[0]->geometry->location->lng;
        $ogt=owghat($month , $day , $longitude , $latitude  , 0 , 1 , 0);
}
function map() {
    //one method of defining globals in a function
    $GLOBALS['ogt'] = owghat($moth, $day, $longitude, $latitude, 0, 1, 0);
    // OR
    //the other way of defining a global in a function
    global $ogt;
    $ogt = owghat($month, $day, $longitude, $latitude, 0, 1, 0);
}
function map() {
    $ogt = owghat($month, $day, $longitude, $latitude, 0, 1, 0);
    return $ogt;
}

$ogt = map(); //defined in global scope.
但你不应该这样做。如果我们想从函数中获取变量,只需从函数中返回:

function map(){          
        $address = mashhad; // Google HQ
        $prepAddr = str_replace(' ','+',$address);
        $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
        $output= json_decode($geocode);
        $latitude = $output->results[0]->geometry->location->lat;
        $longitude = $output->results[0]->geometry->location->lng;
        $ogt=owghat($month , $day , $longitude , $latitude  , 0 , 1 , 0);
}
function map() {
    //one method of defining globals in a function
    $GLOBALS['ogt'] = owghat($moth, $day, $longitude, $latitude, 0, 1, 0);
    // OR
    //the other way of defining a global in a function
    global $ogt;
    $ogt = owghat($month, $day, $longitude, $latitude, 0, 1, 0);
}
function map() {
    $ogt = owghat($month, $day, $longitude, $latitude, 0, 1, 0);
    return $ogt;
}

$ogt = map(); //defined in global scope.

如果您在该函数之外声明
$ogt
,它将是您所做工作的全局。您还可以使用
$ogt
map()
调用函数作为调用的一部分。这真的取决于你在做什么。也可以像在C#中一样将变量声明为public。我从PHP手册中推荐:


不是通过函数,但如果您使用OOP,您可以这样做;但是你的函数不返回任何东西,那么为什么不返回$ogt,然后你可以使用它呢?如果函数返回一个数组,我需要返回每个ogt变量,所以我不能返回它。当然,我想!您的
owghat()
函数可能会返回一个数组,但您的
map()
函数不会返回任何内容。只有在函数定义中将其声明为全局时,它才会是全局的。这就是为什么我链接了范围和可见性主题:)我认为最好完全理解所有选项。