PHP closure function parameters and external variables. PHP closure function passing parameters and using external variables. This article describes the php closure function passing parameters and using external variables. For your reference, refer to the following methods for passing parameters through the PHP closure function and using external variables: php variables
This article describes how to pass parameters in PHP closure functions and use external variables. We will share this with you for your reference. The details are as follows:
Write two methods in the Laravel controller. one is to create a closure function internally, the other is to execute the passed closure function, test the writing method of the closure, and use external variables, and parameters of the closure function. As follows:
// Test the closure for passing parameters and use the external variable public function testClosure ($ t1, $ t2) {$ closure = function ($ param1, $ param2) use ($ t1, $ t2) {echo $ param1. $ param2. $ t1. $ t2 ;}; $ this-> execClosure ('test. closure ', $ closure);} // execute the Closure function protected function execClosure ($ name, closure $ Closure) {echo 'closure func name :'. $ name; echo'
'; $ Closure ('p1', 'P2 ');}
Add a route in routes. php:
The code is as follows: Route: get ('/test/closure/{t1}/{t2}', ['uses '=> 'testcontroller @ testClosure']);
Visit www.example.com/test/closure/hehe1/hehe2
Browser output result:
Closure func name: test. closurep1p2hehe1he2