undefinedfix
Sign in

How to add predefined constants or variables in PHP

jstoobz edited in Wed, 02 Dec 2020

I want to define a constant or variable and use it anywhere, like E_ All or$_ SERVER

4 Replies
Amer
commented on Thu, 03 Dec 2020

Statement to define constants

Global variables can be defined in the global variable array $globals http://php.net/manual/zh/rese...


Looking at the main question, I feel that "environment variable" may be in line with the main problem. When you configure PHP server with nginx, you can add the following instructions

fastcgi_param CUSTOM_ENV_VAR "my_env_value";

After that, as long as the PHP request is accessed through nginx, it can be used in any code$_ SERVER['CUSTOM_ ENV_ Var '] to get the value you need, or add a line to fastcgi's configuration file:

env[CUSTOM_ENV_VAR]="my_env_value"

After that, as long as the PHP files processed by PHP fastcgi can be accessed through getenv ('custom ')_ ENV_ Var '); to get the set value


If the subject really wants to get a similar e instead of defining it himself_ For constants like all, you need to write some C. you can create your own extension (or just find any extension that you can compile) in PHP_ MINIT_ Function uses the following macro to define its own constants

REGISTER_STRINGL_CONSTANT("MY_SITE_VERSION", "1.0", 3, CONST_PERSISTENT);

Then recompile PHP

telulas
commented on Thu, 03 Dec 2020

Constant can be used everywhere, defined by define function.

Variables are not recommended to use global variables. You can use static properties of classes to achieve similar functions.

Mossy
commented on Thu, 03 Dec 2020

There's a way. Here we are php.ini Configure Auto in_ prepend_ Item file

MolnarIstvan
commented on Thu, 03 Dec 2020
  • Constant define
  • Environment variable putenv ()
lock This question has been locked and the reply function has been disabled.