简体   繁体   中英

In WP, can't include_once() same file in both header.php and footer.php

There are 3 .php files:

vars.php

    $foo = 'bar';

header.php:

    include_once("vars.php");
    echo "In header: $foo";

footer.php:

    include_once("vars.php");
    echo "In footer: $foo";

In the above situation, $foo is not set in footer.php. If I remove the include_once() in header.php, then $foo is set in footer.php, but not header.php.

I'm guessing I'm making a silly somewhere, but for the life of me I can't find it.

The ..._once() functions don't care WHERE the include/require occured. Once a file has been included, it'll never been include_once/require_once again anywhere during that particular script execution run.

If you need to include it multiple times in different locations, then don't use the _once() variants.

The reason it's not included a second time is because you told it to include_once - once .

At any rate, $foo should be included if you keep the include_once in header.php. Make sure you're not changing $foo down the road before footer.php.

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