对于 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.
这是针对当前的 PHP 版本 5.3。
关于php - 如何解释 PHP 宏定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6555775/