Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    storing booleans in the config

    Scheduled Pinned Locked Moved Development
    4 Posts 3 Posters 638 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      coreybrett
      last edited by

      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?

      S bmeeksB 2 Replies Last reply Reply Quote 0
      • S
        Steve_B Netgate @coreybrett
        last edited by

        Use:

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

        Als ik kan

        1 Reply Last reply Reply Quote 0
        • bmeeksB
          bmeeks @coreybrett
          last edited by bmeeks

          @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>
          
          1 Reply Last reply Reply Quote 0
          • C
            coreybrett
            last edited by

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.