Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

environment variables - getenv() vs. $_ENV in PHP

What is the difference between getenv() and $_ENV?

Any trade-offs between using either?

I noticed sometimes getenv() gives me what I need, while $_ENV does not (such as HOME).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the php documentation about getenv, they are exactly the same, except that getenv will look for the variable in a case-insensitive manner when running on case-insensitive file systems (like Windows). On Linux hosts it still works as case-sensitive. Most of the time it probably doesn't matter, but one of the comments on the documentation explains:

For example on Windows $_SERVER['Path'] is like you see, with the first letter capitalized, not 'PATH' as you might expect.

Because of that, I would probably opt to use getenv to improve cross-platform behavior, unless you are certain about the casing of the environment variable you are trying to retrieve.

Steve Clay's comment in this answer highlights another difference:

Added getenv() advantage: you don't need to check isset/empty before access. getenv() won't emit notices.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...