Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...