2

I have this function:

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);
}

and i need to use form $ogt in another function,is it possible that declared ogt as a public variable and if it possible how can i do that?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
amin gholami
  • 453
  • 3
  • 10

3 Answers3

2

If you declare $ogt outside of that function, it will be global to the work you are doing. You could also call a function from map() with $ogt as part of the call. It really depends on what you are doing. You can also just declare the variable as public as you could in C#. I would recommend this from the PHP manual:

http://php.net/manual/en/language.oop5.visibility.php

http://php.net/manual/en/language.variables.scope.php

Jesse Williams
  • 653
  • 7
  • 21
1

You can set it as a global in the function:

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);
}

BUT this isn't the way you should go about this. If we want a variable from a function we just return it from the function:

function map() {
    $ogt = owghat($month, $day, $longitude, $latitude, 0, 1, 0);
    return $ogt;
}

$ogt = map(); //defined in global scope.
Evadecaptcha
  • 1,403
  • 11
  • 17
0

you can declare $ogt variable as global variable

Source:

1: http://php.net/manual/en/language.variables.scope.php

2: http://php.net/manual/en/reserved.variables.globals.php

Samit Khulve
  • 154
  • 6