简体   繁体   中英

Sharing variables in PHP

I have the following code in a WordPress theme that I am developing that checks to see if a plugin is active;

if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {

    $WooCommerceState = True;

}

The aim was that once this was set at the top of the file I could use it in the rest of the file to display certain information based on whether $WooCommerceState was True or False so I have;

if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {

    $WooCommerceState = True;

}

<?php if ( $WooCommerceState == True ) { ?>

..do something

<?php } ?>


<?php if ( $WooCommerceState == True ) { ?>

..do something

<?php } ?>

This is all within the same file, but when I view the page I get a

Notice: Undefined variable...

message for the two tests. How can I resolve this?

try this, it's undefined when plugin not active. Should be always initialized before using

$WooCommerceState = is_plugin_active( 'woocommerce/woocommerce.php' );

and remove that first if

-edited after rjdown comment. Thanks-

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