265

I am working on a project in Laravel where I am storing some settings in .env file setting like few parameters for testing purpose and few parameters are for live working so I was just checking that is there any way to comment in .env file of Laravel.

Here is an example

/* Test Settings */
ACCESS_KEY=qwsdr
ACCESS_TOKEN=Bgcvfsx

/* Live Settings */
ACCESS_KEY=985AsdefG
ACCCESS_TOKEN=LFP994kL

3 Answers 3

453

You use hash commenting:

# Test Settings
ACCESS_KEY=qwsdr
ACCESS_TOKEN=Bgcvfsx

# Live Settings
ACCESS_KEY=985AsdefG
ACCCESS_TOKEN=LFP994kL

Documentation: https://github.com/vlucas/phpdotenv#comments

Sign up to request clarification or add additional context in comments.

1 Comment

Also on same line: DEBUGBAR_ENABLED=true # may reveal e.g. MySQL password
36

Please note that as of Laravel 5.8 comment parsing in values has changed.

In Laravel 5.7 an .env file containing ENV_VALUE=foo#bar would evaluate to foo#bar.

In Laravel 5.8 the same .env file would evaluate to foo instead, with #bar being seen as a comment.

To use the # character in a value, double quote the entire value like so ENV_VALUE="foo#bar".

Comments

21

Laravel use the vlucas/phpdotenv package to parse .env file.

So according to the doc, you can comment like this:

# Test Settings
ACCESS_KEY=qwsdr
ACCESS_TOKEN=Bgcvfsx

# Live Settings
ACCESS_KEY=985AsdefG
ACCCESS_TOKEN=LFP994kL

Since Laravel 5.8, you can do something like this:

ENV_VALUE1=foo#bar
ENV_VALUE2="foo#bar"

will return:

env('ENV_VALUE1'); // foo
env('ENV_VALUE2'); // foo#bar

The phpdotenv package that is used to parse .env files has released a new major version, which may impact the results returned from the env helper. Specifically, the # character in an unquoted value will now be considered a comment instead of part of the value:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.