$GLOBALS & global doesn’t work [closed]

In my opinion you shouldn’t use $GLOBALS at all.
add_filter shouldn’t be used the way you are using it.

So there is apply_filter in wordpress that returns a filtered value
such that

$my_variable = apply_filter('my_filter_tag', 'default_value', $arg1);
  • In the above code 'default_value' can be anything.
  • $my_variable will hold the value 'default_value' if no one called add_filter('my_filter_tag', 'my_function_name', 10, 2)

When someone calls add_filter('my_filter_tag', 'my_function_name', 10, 2) what this means is that

$my_variable = my_function_name('default_value', $arg1); Here the value of $my_variable will be equal to the result of the my_function_name because we registered my_function_name to be called every time the value of $my_variable is being set.

To achieve the desired effect described in the post you can do the following:

# This will set the value of $GLOBALS['x'] = 1 by default if there are no hooks called.
# $arg1 is optional but since you specified the value 2 when registering the filter I've added it to the example to explain it
$GLOBALS['x'] = apply_filter('filter_hook', 1, $arg1); 

function my_function($x, $y) {
    # Here this function required two parameters
    # This function is being called by the filter_hook
    # So the value of $x will be equal to 1 ( Which is the default value)
    # $y will be $arg1
    return 2; # Notice that we're returning the value rather than setting it.
}

# Here the value 10 means priority and the value 2 means that the function 
#`my_function` will be given two arguments when it is called, the first argument
# for the called function will always be the 'default_value'
add_filter( 'filter_hook', 'my_function', 10, 2 );

add_action( 'action_hook', 'my_function2', 10, 2 );
function my_function2() {
    echo $GLOBALS['x']; 
}