Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is get_env() treating my boolean environment variable as a string?

Tags:

php

wordpress

I upgraded my Wordpress instance from 4.5 to 4.8, and for some reason my boolean environment variable is now returning as a string of "false" rather than false. Since my PHP version hasn't changed, I'm a bit mystified at the change. More importantly however, about going forward, what's the best way to manage getting booleans into PHP via environment vars?

Here's the .env line I have:

WP_FORCE_SSL_ADMIN=false

Here's the line I had in my wp-config.php that is returning true due to string conversion.

define('FORCE_SSL_ADMIN', getenv('WP_FORCE_SSL_ADMIN'));

Here's the var_dump:

["WP_FORCE_SSL_ADMIN"]=>
string(5) "false"

I know that I can simply refactor the define to account for the string conversion, but I'm bothered that I don't understand what changed when nothing should have. This worked fine on for wordpress version 4.5.

Looking for an answer to explain the best method for passing boolean vars from my environment into PHP, or do I always have to account for the string conversion?

like image 305
gdbj Avatar asked Jan 03 '23 11:01

gdbj


1 Answers

Just in case someone comes here looking for the answer to how I solved it, what I did was simply do a comparison to the string of "true".

define('FORCE_SSL_ADMIN', strtolower(getenv('WP_FORCE_SSL_ADMIN')) === "true");

While this works, it still doesn't feel quite right to me, like I'm missing something.

like image 72
gdbj Avatar answered Jan 14 '23 13:01

gdbj