What's the Difference Between Define and Const in PHP?

preview_player
Показать описание
Difference between Define and Const in PHP

- const defines a constant at compile time
- define defines them at run time
- define allows you to define case insensitive constants, const does not (they are always case sensitive)
- const defines constants in the current namespace
- define must be passed the namespace
- consts are done at compile time as we said, and are thus a bit faster
- 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, if statements or try/ catch blocks.
- Const can be used to define constants on classes, define cannot
- When using the const keyword, only scalar data (boolean, integer, float and string) can be contained in constants prior to PHP 5.6. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant.
- It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.
Рекомендации по теме
Комментарии
Автор

Thanks! I‘ve a test tomorrow and you really helped me to understand the difference in just 2 minutes :))

blackyandsamtpfote
welcome to shbcf.ru