Home  >  Article  >  Backend Development  >  PHP constant problem: Comparison between const and define, please give me some advice

PHP constant problem: Comparison between const and define, please give me some advice

WBOY
WBOYOriginal
2016-12-01 00:56:56809browse

Only for the speed issue, let’s not mention the usage for now. I saw someone on the Internet saying that Const belongs to structural syntax, and its speed will be several times higher than the define function. I need a professional explanation. Is this true? Now that const can indeed be used outside a class, will everyone give it priority when defining constants?

Reply content:

Only for the speed issue, let’s not mention the usage for now. I saw someone on the Internet saying that Const belongs to structural syntax, and its speed will be several times higher than the define function. I need a professional explanation. Is this true? Now that const can indeed be used outside a class, will everyone give it priority when defining constants?

For speed comparison, I suggest you write a few lines of code to test it yourself, which is much better than asking here.

  • Speed:

    • If const is faster, it is also a subtle difference. It doesn’t make much sense to consider this

    • If const was better, the documentation would recommend you to use it, but it’s not, so don’t worry about it

  • Will you give priority to using const:

    • According to the current differences and personal habits, just follow the various usage regulations

1. Now both methods can use expressions;
2.const can define arrays, define can only define scalars

Even if the syntax structure is faster than the function call, the difference between const and define() is insignificant. Instead of worrying about this trivial performance issue, it is better to optimize the table structure and SQL statement.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn