3 hours ago · Tech · 0 comments

The humble .env file is a useful and low-tech way of storing persistent environment variables. Drop the file on your server and let your PHP scripts consume it with glee. But consume it how? There are lots of excellent parsing libraries for PHP. But isn't there a simpler way? Yes! You can use PHP's parse_ini_file() function and it works. But… .env and .ini have subtly different behaviour which might cause you to swear at your computer. Let's take this example: # This is a comment USERNAME="edent" Run $env = parse_ini_file( ".env" ); and you'll get back an array setting the USERNAME to be "edent". Hurrah! Works perfectly. Ship it! But consider this: # This is a comment USERNAME="edent" # Don't use an @ symbol here. It will happily tell you that the username is "edent# Don" WTAF? Here's the thing. The comment character for .ini is not # - it's the semicolon ; Let me give you some other examples of things which will fuck up your parsing: # Documentation at https:/example.com/?doc=123…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.