php - 如何解释 PHP 宏定义?

标签 php macros c-preprocessor

对于 C/C++,我认为预处理器在编译器编译程序之前进行宏处理,这就是为什么宏名称必须是文字或绝对名称,不允许声明宏名称的变量。

对于 PHP,我们可以这样声明:DEFINE($name, $value); 也就是说,解释器首先解释变量,然后定义宏,即像运行时宏处理。

这实际上让人怀疑,这真的是宏处理吗? 如果不是那么引入像'define'这样的关键字的目的是什么?具有无法更改的值?

或者如果有宏处理,那么谁能解释一下它是如何工作的?

最佳答案

为了完整起见,一个在编译时定义的常量:

const MY_CONSTANT = 'something';

As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops or if statements.

From the PHP Manual .

这是针对当前的 PHP 版本 5.3。

关于php - 如何解释 PHP 宏定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6555775/

相关文章:

lisp - 帮我写一个 Clojure 宏,它会自动将元数据添加到函数定义中

macros - 宏扩展到相同的运算符

c++ - 如何逐步查看宏扩展?

c++ - 带循环的宏在 C/C++ 中的实际工作方式

c - 将预处理器指令嵌入到函数名称中

php - codeigniter:更改 PHP 数组

php - 存储复选框数据

php - 为什么找不到我的界面?

php - 将 DirectoryIterator 转换为可映射数组?

c - 测试预处理器符号是否在宏中定义