{"id":1792,"date":"2012-12-08T13:37:26","date_gmt":"2012-12-08T13:37:26","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1792"},"modified":"2012-12-08T13:37:26","modified_gmt":"2012-12-08T13:37:26","slug":"php-function-use","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2012\/12\/08\/php-function-use\/","title":{"rendered":"PHP &#8211; function use"},"content":{"rendered":"<p>As with the previous post about the use method within php (<a href=\"http:\/\/www.codingfriends.com\/index.php\/2012\/07\/11\/php-use\/\" title=\"here\">here<\/a>), you are also able to use the use method attached to a callable function as in <\/p>\n<pre lang=\"php\">\r\nfunction ($parameter) USE ($somevalue)\r\n<\/pre>\n<p>So you are able to use any variables that are within scope within the new function, below is an example where I am using the <a href=\"http:\/\/uk1.php.net\/array_walk\" title=\"array walk\">array walk<\/a> methods callback (which means call a function that is available).  The function is using the values within the array ($arrays 4 and 5) and adding them to the $value variable that is passed within the use parameters (here I am using the &#038; symbol which means to pass in reference and not just a copy of the value (you are able to alter the actual parameter passed), so in this instance the $value starts of with 1, then adds 4 and then 5 to it which in turn the result is 10.<\/p>\n<pre lang=\"php\">\r\n$value = 1;\r\n$callback = function ($one) use (&$value) {\r\n\t$value += $one;\r\n};\r\n$arrays = array(4,5);\r\narray_walk($arrays, $callback);\r\nprint_r(\"value : \" .$value);\r\n<\/pre>\n<p>And the output would be.<\/p>\n<pre lang=\"bash\">\r\nvalue : 10\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As with the previous post about the use method within php (here), you are also able to use the use method attached to a callable function as in function ($parameter) USE ($somevalue) So you are able to use any variables that are within scope within the new function, below is an example where I am &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2012\/12\/08\/php-function-use\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP &#8211; function use<\/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":[17],"tags":[65,403,391],"class_list":["post-1792","post","type-post","status-publish","format-standard","hentry","category-php","tag-function","tag-php","tag-use"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1792"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1792\/revisions"}],"predecessor-version":[{"id":1794,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1792\/revisions\/1794"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}