How to pass hook variable to function?

You can’t use an action or hook within a function in the way you are expecting. Actions and hooks are about delayed execution You are attaching code that will execute at a later point. After an action is added, your function will continue as normal.

Then when the user is actually being registered, your code will run. You will never get back to your original function though.

You might want to consider using multiple actions, and global variables. In the first action we can set the global:

add_action( 'user_register' .....
global $myvariable;
    .
    .
    .
echo $user_id;
var_dump ($_POST);
$myvariable="a Value";
    .
    .
    .
}

Then you can have another action that you fire somewhere else later. Here we can call on this global data:

add_action('wp_footer', function() {
    global $myvariable;
    echo $myvariable;
});

tech