Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: using an environment variable as a reference to a local constant

Tags:

php

My code:

const T = 'test';
const B = 'boat';

$const_var = getenv('FOO');

In my VirtualHost section, I have:

SetEnv FOO T

Obviously $const_var evaluates to the character T.

What I want to do is to be able to use the value of local const T, by using the value of the environment variable as a reference. Is there a way to do that?

like image 442
EastsideDev Avatar asked Nov 29 '12 00:11

EastsideDev


People also ask

Where do I put constants in PHP?

Define your constant in your top . php file, that will be included in all the other scripts. It may be your front controller, your config file, or a file created for this single purpose.

What does $_ ENV mean in PHP?

$_ENV is another superglobal associative array in PHP. It stores environment variables available to current script. $HTTP_ENV_VARS also contains the same information, but is not a superglobal, and now been deprecated. Environment variables are imported into global namespace.


1 Answers

You can use the constant() function to resolve the value indirectly.

 print constant($_ENV["FOO"]);

Or in your case $const_var as parameter.

like image 88
mario Avatar answered Sep 23 '22 21:09

mario