Bhargav Lalaji
Bhargav Lalaji

Reputation: 81

How to read system variable with php file

I am trying to fetch the system variable that I have set through the Kubernetes yaml and need to read into php file that I have created. Somehow I am getting blank value.

I have set MYSQL_USER and MYSQL_PASSWORD over yaml file which is successfully reflect inside the container. Now I need to read those variable with the php file.So, I have created one env.php to confirm as below,

<?php
$db_host = getenv('MYSQL_HOST', true) ?: getenv('MYSQL_HOST');
$db_name = getenv('MYSQL_DATABASE', true) ?: getenv('MYSQL_DATABASE');
$db_user = getenv('MYSQL_USER', true) ?: getenv('MYSQL_USER');
$db_pwd  = getenv('MYSQL_PASSWORD', true) ?: getenv('MYSQL_PASSWORD');

echo "db_host: {$db_host}<br>";
echo "db_name: {$db_name}<br>";
echo "db_user: {$db_user}<br>";
echo "db_pwd: {$db_pwd}<br>"; 
?>

In this case, I am getting blank value as below

db_host:
db_name:
db_user:
db_pwd: 

is there any other way that I can get those variable's value in PHP ?

Upvotes: 0

Views: 142

Answers (1)

Nothing could be simpler, Watson:

$ Zumba=King php -r 'print_r(getenv("Zumba") . PHP_EOL);'
King

The second parameter does not mean what you think it does, but you can do this:

$ php -r 'print_r((getenv("Zumba") ?: "Queen") . PHP_EOL);'
Queen

Upvotes: 1

Related Questions