storing booleans in the config



  • When I do the following...

    $config['installedpackages']['zerotier']['enabled'] = true;
    

    It shows up in config.xml as...

    <zerotier>
      <enabled></enabled>
    </zerotier>
    

    However, if I use it in an if statement like so...

    if($config['installedpackages']['zerotier']['enabled']) {
        print("Hello Corey");
    }
    

    it evaluates to false.

    Am I doing something wrong?


  • Developer Netgate

    Use:

    if(isset($config['installedpackages']['zerotier']['enabled'])) {
        print("Hello Corey");
    }
    


  • @coreybrett said in storing booleans in the config:

    When I do the following...

    $config['installedpackages']['zerotier']['enabled'] = true;
    

    It shows up in config.xml as...

    <zerotier>
      <enabled></enabled>
    </zerotier>
    

    However, if I use it in an if statement like so...

    if($config['installedpackages']['zerotier']['enabled']) {
        print("Hello Corey");
    }
    

    it evaluates to false.

    Am I doing something wrong?

    My preference in the packages I maintain is to actually set the "enabled" item to a string value such as "on" or "off" (you could also use "yes" or "no"). That way when looking through the XML you can quickly tell at a glance if the item is really enabled or not. With the way you have it now, you are using the existence of the array item to indicate "on". While that works, I prefer my method as it makes it easier for future programmers to contribute to your package or take over maintenance if you choose to stop being the package maintainer.

    So I would do this:

    $config['installedpackages']['zerotier']['enabled'] = "yes";
    
    <zerotier>
      <enabled>yes</enabled>
    </zerotier>
    


  • this is exactly what i ended up doing, i kept having odd problems when trying to use bools, thanks for the feedback