CodeIgniter Forums
env file and boolean values ​​such as true or false - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: env file and boolean values ​​such as true or false (/showthread.php?tid=84896)



env file and boolean values ​​such as true or false - serialkiller - 11-21-2022

Inside the .env file, how should I deal with boolean variables?
In the original file, for example, I find variables set to true and one set to 'true'.

Code:
honeypot.hidden = 'true'


I've set my variable to both true and 'true', but it's always treated as a string.

I'm using getenv() to retrieve the value, is there maybe some other function that returns a boolean instead of a string in these cases?


RE: env file and boolean values ​​such as true or false - kenjis - 11-21-2022

getenv() returns string or false.
https://www.php.net/manual/ja/function.getenv.php

You can get the value as bool if you use Config class.
See https://codeigniter4.github.io/CodeIgniter4/general/configuration.html#configuration-classes-and-environment-variables


RE: env file and boolean values ​​such as true or false - serialkiller - 11-21-2022

(11-21-2022, 02:54 AM)kenjis Wrote: getenv() returns string or false.
https://www.php.net/manual/ja/function.getenv.php

You can get the value as bool if you use Config class.
See https://codeigniter4.github.io/CodeIgniter4/general/configuration.html#configuration-classes-and-environment-variables

I think there was some helper that could handle this, unfortunately it's not a variable to configure a class, so either I use 0/1 or I have to do a string comparison, thanks kenjis


RE: env file and boolean values ​​such as true or false - kenjis - 11-21-2022

You can use env() in CI4.
Try it.


RE: env file and boolean values ​​such as true or false - serialkiller - 11-21-2022

(11-21-2022, 03:12 AM)kenjis Wrote: You can use env() in CI4.
Try it.

I tried it and it works, where can I find it in the documentation?

EDIT: okay, found it

https://codeigniter.com/user_guide/general/common_functions.html?highlight=env#env

it would be better to mention it in the configuration section too in my opinion

Thanks again kenjis


RE: env file and boolean values ​​such as true or false - parisiam - 03-21-2023

I think the solution is to let the Dotenv value to empty.

Let's say I have a boolean Dotenv variable such as email.DSN. To set it to false, I use:
Code:
# Use the following to set the value to false
email.DSN =

If I set any other value, it will be considered as true, event if I set it to false.
For example the following values will result in true in the code.
Code:
# All the following values will be considered true
email.DSN = false
email.DSN = 0
email.DSN = whatever
email.DSN = true
email.DSN = 1

Note: I don't think using env() function is a good solution if the Dotenv variable corresponds to a config value.
In my example, there is a 'DSN' variable in the app/Config/Email.php file:
PHP Code:
public bool $DSN false
As the env() function does not read the config file, if I remove the value of email.DSN from the Dotenv file, then env('email.DSN') will return null and not the value that is in the config file.