Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Functions Function Returns and More PHP Closures

Abdullah Althobetey
Abdullah Althobetey
18,216 Points

PHP Closures

So what are the benefits of using closures? We can do the same thing with normal functions and using the keyword global instead of use() Or did I miss something?.

2 Answers

Tomasz Budny
Tomasz Budny
14,588 Points

If I understand this correctly if you will use the keyword global you will be able to change the variable outside of the function, but if you will just inherit the variable by passing it through use($var) than you will just get the global variables value from the moment of the functions definition to use inside your function under the same variable name but you are not going to be able to change the global variable it self. So its safer I guess.

There is also an option to "inherit by-reference" like so - use(&$var). This will let you use the value of a variable from when the function is called not from when it is defined.

BUUUUT that's just how I understand it. Don't take my word for it, just read this:

http://php.net/manual/en/functions.anonymous.php

Does your suggested method support namespacing? Because if not.. that's a huge advantage!