从回调函数中检索变量

So I have a callback function set up and I figured out the most efficient way for my code to run and to not need to repeat queries would be to get a variable, made by the callback function and use it out side of the function.

So I have a function like so:

 // Not as important.
 myCallbackFunction(callable $callback) {
   // Some stuff executed.
   $callback($VARIABLE);
 }
 // Front end of the code
 $OBJECT->myCallbackFunction(function($RANDOM)) {
   $id = 1; // Example number that is given.
 });

So I needed the $id variable from the callback function, so I added this code in the front end:

global $id;

Trying to use that variable comes as if it is empty but it is there.

Is there a another way to get variables from callback functions? I'm new to callbacks so I'm sorry if this has been asked before or is a simple fix.

You need to return the variable from your callback, then it will be available to code that invokes it, e.g.

<?php
function runCallback($callback) {
    $id = $callback();

    echo $id; // 1234
}

runCallback(function() {
    $id = 1234;
    return $id;
});