简体   繁体   中英

Global variable isn't accessible inside my Wordpress function. Why not?

I am working in wordpress, and have a function in functions.php. This is meant to set a number of variables based on the context the variable is used in. But there's a problem.

I am using the function in an included template file, and the function is intended to work with variables on the page that template file is included into. I declare all the variables as global inside my function, but the function doesn't recognize the values of the variables. I don't understand why this is happening, because I am certain that the variable scope is being used properly.

To clear up confusion, I have included a simplified code example below, showing the three files involved in this issue. If anybody has any idea why this is happening, I would be delighted to hear it. I am interested in understanding the reasons why it is happening, more than looking for a fix.

functions.php

function set_variables() {
    global $data;
    print_r($data);
}

included_file.php

set_variables();
(Code that sets other variables and works with HTML)

template_file.php

$data = "Test";
include "included_file.php";

The result of the code above is nothing--I can't get the function in functions.php to recognize the variable defined in template_file.php. However, if I define the $data variable in functions.php, it works.

Like I said, this baffles me since it seems to contradict how declaring global variables within a function is supposed to work. How am I getting it wrong?

It looks like you misspelled the calling function:

set_variable() is not the same as set_variables()

Please note the following from PHP about including files:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

See: http://php.net/manual/en/function.include.php

@zerkms - Thanks very much for answering my question. Turns out all I had to do was declare the variable as global in the file where it was defined.

So, in the example given above, the solution is as follows:

functions.php

function set_variables() {
    global $data;
    print_r($data);
}

included_file.php

set_variables();
(Code that sets other variables and works with HTML)

template_file.php

global $data = "Test";
include "included_file.php";

I just assumed that the variable declared in the template_file.php was in the global scope, but I suppose it wasn't. Still a bit fuzzy on the whys, but I know the code worked, and I'm really happy about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM