Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php pass variable to anonymous function


<?php
$message = 'hello';

// No "use"
$example = function () {
    var_dump($message);
};
$example();

// Inherit $message
$example = function () use ($message) {
    var_dump($message);
};
$example();

// Inherited variable's value is from when the function
// is defined, not when called
$message = 'world';
$example();

// Reset message
$message = 'hello';

// Inherit by-reference
$example = function () use (&$message) {
    var_dump($message);
};
$example();

// The changed value in the parent scope
// is reflected inside the function call
$message = 'world';
$example();

// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    var_dump($arg . ' ' . $message);
};
$example("hello");
?>

Comment

PREVIOUS NEXT
Code Example
Php :: PHP | get client ip 
Php :: group in route in laravel 
Php :: wp revisions config 
Php :: How to send data from PHP to Python 
Php :: Allowed memory size of 1610612736 bytes exhausted 
Php :: laravel get last month records 
Php :: php in array 
Php :: convert text to slug php 
Php :: Creating a Basic Route in Laravel 8 
Php :: php sort array by key 
Php :: php konstanten 
Php :: laravel query get big table records 
Php :: how to delete all data from table in php 
Php :: non negative integer validation laravel 
Php :: laravel validation max string length 
Php :: laravel array remove key 
Php :: laravel blade file naming conventine 
Php :: php post 
Php :: debug $_POST 
Php :: sortbydesc on a collection laravel 
Php :: ubuntu set alternatives 
Php :: carbon two day ago 
Php :: php remove prefix from string 
Php :: laravel group by on subquery 
Php :: check if the form is submitted php 
Php :: php json_encode without square brackets 
Php :: malformed utf-8 characters possibly incorrectly encoded php 
Php :: a facade root has not been set phpunit 
Php :: upload_max_filesize 
Php :: carbon parse timestamp 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =