Welcome Guest, Not a member yet? Register   Sign In
env file and boolean values ​​such as true or false
#1

(This post was last modified: 11-21-2022, 02:21 AM by serialkiller.)

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?
Reply
#2

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/CodeIgnit...-variables
Reply
#3

(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/CodeIgnit...-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
Reply
#4

You can use env() in CI4.
Try it.
Reply
#5

(This post was last modified: 11-21-2022, 08:09 AM by serialkiller.)

(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/gener...ht=env#env

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

Thanks again kenjis
Reply
#6

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB