Another solution without getenv()
While the top rated answer answers the question, I want to add a solution that works both remote and local with the Php Dotenv Package:
1.Grab the variables you need with heroku config -a <app>
, here the mysql
string:
CLEARDB_DATABASE_URL: mysql://<user>:<password>@<host>/<db>?reconnect=true
2.Prepare your app locally
composer.json
{
"require": {
"vlucas/phpdotenv": "^5.4"
}
}
.env
host_heroku= ''
the index
index.php
include "config.php";
// Db connection is built
the config
config.php
include realpath(__DIR__ . '/vendor/autoload.php');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();
$db_hostname = $_ENV['host_heroku'];
3.Commit and push to heroku