In your .env file: TEST_API_KEY='xxx'
In your config/application.php: env('TEST_API_KEY'). You need to add this after Dotenv has loaded your .env file (after line 43).
Is the PHP file a standalone file that is called directly like https://site.void/file.php ? If so, WordPress is probably not loaded and neither is Env etc. and you will have to include them yourself.
I would start by dumping $test_api to see if it gets the value you expect and if not work from there.
I would also guess that for example const TEST_API_KEY = "$test_api"; should be const TEST_API_KEY = $test_api; without the quotes, but then again PHP works in mysterious ways
Thanks for the replies @folbert and @tombro - yes the PHP file is standalone hence I was trying same as application.php after open php tag:
use Roots\WPConfig\Config;
use function Env\env;
But that seemed to break the file loading so presume I haven’t called something correctly - @folbert yes you are right about quotes I was playing around with a string then trying the var but left the quote in where I copied/pasted.
So I added the vars to the .env and config/application.php after dotenv so just wondering what the correct syntax is to call the variable then in the standalone php file?
Should it just be as before? As it still doest seem to work.
$_ENV['TEST_API_KEY'];
Sorry I’m no expert in PHP but have a good enough understanding of it just this is a bit of a learning curve in terms of adding custom env variables.
Bedrock is built load all PHP through WordPress, so a standalone PHP file will never be executed in a Bedrock context.
If your file is in site/web then you may be able to just require_once( 'wp-config.php' ) at the top of it and have access to all of Bedrock’s variables, dependencies, etc.