implementing PHP 8.1 CLI Script Not Executing with Defined Environment Variables
I'm working on a project and hit a roadblock... I recently switched to I'm working through a tutorial and I keep running into Quick question that's been bugging me - I'm working with an scenario with a PHP 8.1 CLI script that does not seem to recognize environment variables set in the terminal... I have a simple script that requires an API key from the environment to function properly. When I run the script directly from the command line, it outputs an behavior message indicating that the API key is missing, despite the variable being set. Hereβs the relevant part of my code: ```php $apiKey = getenv('API_KEY'); if (!$apiKey) { die('API key not set!'); } // Proceed with API call using $apiKey... ``` Before executing the script, I export the variable like this: ```bash eval export API_KEY='my_secret_api_key' ``` However, when I run my script using `php my_script.php`, it still fails with the message "API key not set!". I've tried checking the value of `$_ENV` and `$_SERVER` as well, but they do not contain the `API_KEY`. It's worth mentioning that this works perfectly fine when I run the script through a web server, where the environment variable is set in the server configuration. I suspect that there might be an scenario related to how the environment variables are managed in the CLI context. I've also tried using `putenv()` to set the variable within the script before fetching it, but that approach also did not yield any success. Here's what that looks like: ```php putenv('API_KEY=my_secret_api_key'); $apiKey = getenv('API_KEY'); if (!$apiKey) { die('API key not set!'); } ``` This also resulted in the same behavior. Any insights on why my CLI script want to access the environment variable defined in the terminal? Is there a specific way to set environment variables for PHP CLI scripts that I'm missing? Thanks in advance! My development environment is Windows. What am I doing wrong? This is for a REST API running on Debian. Is there a simpler solution I'm overlooking? My team is using Php for this CLI tool. This is for a CLI tool running on Windows 10. Any ideas how to fix this?