Access global variable in function

I have a first php file with a few variables in it that are defined outside of a function. I am trying to find a way to use these variables in a second php file in a function. Since I read that it is not recommended to use the global keyword I’ve tried to pass one of the variables as a parameter as a test based on one of the answers here but without success. Here’s a simplified version of my code at the moment:

Vars.php with:

$test = 'test';

Functions.php with:

include 'Vars.php';

function add_content($test){
    echo 'This is a '. $test;
add_action( 'woocommerce_single_product_summary', 'add_content', 15 );

I am not sure if this matters or not but as you can see this is for a wordpress website. If anyone knows how to do this, that would be highly appreciated. It would feel redundant to have to define all the variables again in the function itself.

Read more here: Access global variable in function

Leave a Reply

Your email address will not be published. Required fields are marked *