Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

martinszeltins's avatar

How to pass a variable to a function with callback?

I could not find anywhere how to pass a variable to my function that uses a callback.

This is giving me an error

NOTICE Undefined variable: data on line number 2
function doIt($callback) { 
    $callback($data); 
}


doIt(function($data) {
    echo 'Anonymous function: ' . $data;
}); 
0 likes
8 replies
DavidPetrov's avatar

What is $data? What you have shown here is just a definition of a parameter your anonymous function is expecting, but you don't have it anywhere inside the body of the doIt() function that's actually using it. That's what the error is telling you. If you have $data defined inside doIt(), everything shall work as expected.

Snapey's avatar

its hard to find something like this when you don't know what to search for.

You can pass values to the closure with use

doIt(function() use($data) {
    echo 'Anonymous function: ' . $data;
});

Stil not sure what its called

martinszeltins's avatar

@SNAPEY - Hmm, still getting an error


NOTICE Undefined variable: data on line number 7

WARNING Missing argument 1 for {closure}(), called in /home4/phptest/public_html/code.php70(5) : eval()'d code on line 4 and defined on line number 7
Anonymous function!

Code:

function doIt($callback) { 
    $callback(); 
} 

doIt(function($data) use ($data) {
    echo 'Anonymous function!' . $data;
});
Snapey's avatar

you are not passing anything to the callback, but expecting it in doIt

check my example

martinszeltins's avatar

@SNAPEY - I got it to work. However I have seen code without the use keyword like this. How do they do it?

function doIt($callback) { 
    $callback(); 
}

$data = 'my data here...';

doIt(function($data) {
    echo 'Anonymous function!' . $data;
});

This is giving me these errors:

WARNING Missing argument 1 for {closure}(), called in /home4/phptest/public_html/code.php70(5) : eval()'d code on line 4 and defined on line number 9

NOTICE Undefined variable: data on line number 10
leandromatos's avatar

@MARTINZELTIN - Both approaches work well:

function doIt($callback)
{
    $callback();
}

$data = 'my data here...';

doIt(function () use ($data) {
    echo 'Anonymous function!' . $data;
});
$data = 'my data here...';

$doIt = function () use ($data) {

    echo 'Anonymous function! ' . $data;
};

$doIt();
kundefine's avatar
Level 2

Why not u use the second argument to pass the data

function doIt($callback, $data) {
    $callback($data);
}



doIt(function($data) {
    echo 'Anonymous function: ' . $data;
}, "Your data will go here");
1 like
martinszeltins's avatar

@KUNDEFINE - Yes!!! Thank you! That's what I was looking for! Like Snapey said, it's hard to find something when you don't know what to search for.

function doIt($data, $callback) {
    $callback($data);
}


doIt('my data', function($data) {
    echo 'Anonymous function: ' . $data;
});

Please or to participate in this conversation.