「インポート」は、.env を読み取って 配列や環境変数に反映 することとして整理します。
function env_import(string $path): array {
if (!is_file($path)) return [];
$out = [];
$lines = file($path, FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) continue;
$pos = strpos($line, '=');
if ($pos === false) continue;
$key = trim(substr($line, 0, $pos));
$val = trim(substr($line, $pos + 1));
// クォート除去
if ((str_starts_with($val, '"') && str_ends_with($val, '"')) || (str_starts_with($val, "'") && str_ends_with($val, "'"))) {
$val = substr($val, 1, -1);
}
$out[$key] = $val;
}
return $out;
}
$vars = env_import(__DIR__ . '/.env');
// 必要なら putenv / $_ENV / $_SERVER に反映(案件ポリシーに合わせる)
Node/Python では dotenv 系ライブラリで読み込むのが一般的です(dotenv, python-dotenv など)。