How efficient is define in PHP?

前端 未结 10 1380
不思量自难忘°
不思量自难忘° 2020-12-15 18:38

C++ preprocessor #define is totally different.

Is the PHP define() any different than just creating a var?

define(\"SETTING         


        
10条回答
  •  遥遥无期
    2020-12-15 18:57

    Here are the differences, from the manual

    • Constants do not have a dollar sign ($) before them;
    • Constants may only be defined using the define() function, not by simple assignment;
    • Constants may be defined and accessed anywhere without regard to variable scoping rules;
    • Constants may not be redefined or undefined once they have been set; and
    • Constants may only evaluate to scalar values.

    For me, the main benefit is the global scope. I certainly don't worry about their efficiency - use them whenever you need a global scalar value which should not be alterable.

提交回复
热议问题