How to configure the Database variables via ENV vars

I’ve been trying to set piwik’s database variables from env vars because the database is subject to change and it will ease my deployment process. I haven’t been able to work anything out yet. I’ve tried the below, hoping that the config.ini.php process would be interpreted at runtime such that it could read out the database variables from the system ENV, but it gives me an error.

; <?php exit; ?> DO NOT REMOVE THIS LINE
; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.
<?php 
     $db_host = $_ENV['DB_HOST'];
    $db_user= $_ENV['DB_USERNAME'];
    $db_pass= $_ENV['DB_PASSWORD'];
    $db_name = $_ENV['DB_NAME'];
    $db_port = $_ENV['DB_PORT'];
?>

[database]
host = "<?php echo $db_host; ?>"
username = "<?php echo $db_user; ?>"
password = "<?php echo $db_pass; ?>"
dbname = "<?php echo $db_name; ?>"
tables_prefix = "<?php echo $_ENV['DB_PREFIX']; ?>"
port = "<?php echo $db_port; ?>"
charset = "utf8"

I eventually dug up this post about doing it https://forum.matomo.org/t/database-config-from-environment-variable-mysql-url/12769 which lead to another post here https://forum.matomo.org/t/deploy-to-containerized-systems-eg-heroku/12115 but neither had enough detail to get me the rest of the way with this project.