简体   繁体   中英

Unable to access user defined environment variables in php

I have defined my own environment variable in windows for holding a folder path.

example:

IMG = C:\\xampp\\htdocs\\img

I am trying to get value of IMG variable in php in following way

var_dump(getenv('MLINK_IMAGES'))

But it just dumps bool(false). But I am able to access pre-defined environment variables

var_dump(getenv('Path'))

It gives me proper value.

So does it require any configuration in php.ini or I am accessing it wrong way.

Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.

The library is available here: https://github.com/jpcercal/environment

Setup your environment variables, after if you are using a web server (restart your Apache Server or PHP Built-in Server or Nginx) else if you are running a cli application close and reopen the terminal program to load your new environment variables to operational system.

And to get the values from environment variable with PHP use my library (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:

<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));

Enjoy it.

The way the question is worded, you are using windows environmental variables. Windows and the web server's environmental variables are not the same.

To store an environmental variable in PHP like the example above:

putenv("IMG=C:\\xampp\\htdocs\\img");

To see a list of all environmental variables as you debug:

echo phpinfo();

Lastly, don't overwrite the path variable for your own use. It is used by the web server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM