{"id":306,"date":"2014-08-11T22:22:58","date_gmt":"2014-08-12T06:22:58","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=306"},"modified":"2014-08-11T22:33:16","modified_gmt":"2014-08-12T06:33:16","slug":"what-are-php-lambdas-and-closures","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=306","title":{"rendered":"What are php Lambdas and Closures?"},"content":{"rendered":"<p>http:\/\/culttt.com\/2013\/03\/25\/what-are-php-lambdas-and-closures\/<\/p>\n<p>What is a Lambda?<br \/>\nA Lambda is an anonymous function that is can be assigned to a variable or passed to another function as an argument. Because the function has no name, you can\u2019t call it like a regular function. Instead you must either assign it to a variable or pass it to another function as an argument.<\/p>\n<p>Lambdas are useful because they are throw away functions that you can use once. Often, you will need a function to do a job, but it doesn\u2019t make sense to have it within the global scope or to even make it available as part of your code. Instead of having a function used once and then left lying around, you can use a Lambda instead.<\/p>\n<p>What is a closure ?<br \/>\nA Closure is essentially the same as a Lambda apart from it can access variables outside the scope that it was created.<br \/>\n<code>\/\/ Create a user<br \/>\n$user = \"Philip\";<\/p>\n<p>\/\/ Create a Closure<br \/>\n$greeting = function() use ($user) {<br \/>\n  echo \"Hello $user\";<br \/>\n};<\/p>\n<p>\/\/ Greet the user<br \/>\n$greeting(); \/\/ Returns \"Hello Philip\"<\/code><\/p>\n<p>As you can see above, the Closure is able to access the $user variable. because it was declared in the use clause of the Closure function definition.<\/p>\n<p>If you were to alter the $user variable within the Closure, it would not effect the original variable. To update the original variable, we can append an ampersand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/culttt.com\/2013\/03\/25\/what-are-php-lambdas-and-closures\/ What is a Lambda? A Lambda is an anonymous function that is can be assigned to a variable or passed to another function as an argument. Because the function has no name, you can\u2019t call it like a regular function. Instead you must either assign it to a variable or pass it to another &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=306\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">What are php Lambdas and Closures?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306"}],"version-history":[{"count":3,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":309,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions\/309"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}