1

I am trying to generate lat and long cordinates for about a 1000+ locations, I know how to get them in a range and everything but I was wondering if I can do something like this.

for ($i = 0; $i < 10; $i++) { $lat = function () { $float = rand(0, 99999) / 99999; $lat = rand(30, 32); $lat = $lat + $float; return $lat; }; $long = function () { $float = rand(0, 99999) / 99999; $long = rand(74, 76); $long = $long + $float; return $long; }; print_r($lat); } 

The code didn't throw the desired result instead, I'm getting

closureObject() 

There's no error but I can't seem to get it to work, any help would be wonderful, I've tried reading the documentation but doesn't explain anything related to this.

Can this work?

4
  • Well, what output did you expect if you print_r() a function definition, so a closure object? Commented Oct 4, 2016 at 13:19
  • Define the closures outside of the loop, and call them inside. As it is, you're not really doing anything. Commented Oct 4, 2016 at 13:20
  • Remember, a closure is a function - you need to call it to get a value. And if all you're doing in the code above is generating random numbers, why bother with a closure at all? Commented Oct 4, 2016 at 13:21
  • 1
    You aren't using the closures, you just defined them. Try: print_r($lat()); and you should see the result. As others have mentioned, why not just define 2 functions then use those functions in your loop? (Could even be 1 function if you see the pattern in them). Commented Oct 4, 2016 at 13:37

2 Answers 2

1

You need to actually call the Closure function you defined like: echo $lat(); If that is what you are trying to do.

As others have mentioned you could define a function the traditional php way. Something like:

function getCoord($min, $max) { $float = rand(0, 99999) / 99999; $coord = rand($min, $max); $coord = $coord + $float; return $coord; } for ($i = 0; $i < 10; $i++) { echo "Lat: " . getCoord(30, 32) . " Long: " . getCoord(74, 76); } 
Sign up to request clarification or add additional context in comments.

4 Comments

It's an object, echo only works with strings. I've tried it.
Yes, $lat is a Closure which is an object that is callable. So when you execute: $lat(); it will return a random float value. Notice how I added ()to the end of the variable?
I know about the tradition function, I was wondering if this can work.
It does work, open up your code and instead of print_r($lat); try: print_r($lat()); Again notice the added parentheses?
1

A closure is an object that can be called like a function. As you can see from the output, when you use $lat in print_r($lat);, it is not the result of calling the closure, it is the closure object itself. (Defined by $lat = function () {... - see example 2 in the PHP documentation for anonymous functions), . If you want to get that result, you have to call it with (), just like any normal function call.

print_r($lat()); 

As it is currently, the closures aren't really necessary, but for an example of how to define and use a closure, you could do like this to eliminate the repeated code:

// assign the anonymous function to $coord $coord = function($a, $b) { $float = rand(0, 99999) / 99999; $coord = rand($a, $b); return $coord + $float; }; for ($i = 0; $i < 10; $i++) { // use $coord to generate coordinates for a point $point = [$coord(30, 32), $coord(74, 76)]; var_dump($point); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.