![]() Namespace A\ B\ C // To define the constantĬonst constants can also be arrays, whileĭefine() does not support arrays yet. const defines a constant in the current namespace, while define() has to be passed the full namespace name:.It’s a language construct instead of a function and also is consistent with how you define constants in classes. ![]() Now let’s look at the reason why I personally always use const unless one of the above situations occurs: consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument: define( 'FOO', 'BAR', true) Įcho FOO // BAR echo foo // BAR So, that was the bad side of things. Since PHP 5.6 constant expressions are allowed in const as wellĬonst BIT_5 = 1 << 5 // valid since PHP 5.6, invalid previouslyĭefine( 'BIT_5', 1 << 5) // always valid for ( $i = 0 $i < 32 $i)Ĥ. const accepts a static scalar (number, string or other constant like true, false, null, _FILE_), whereas define() takes any expression. Why would you want to do that anyways? One common application is to check whether the constant is already defined: if (! defined( 'FOO'))Ģ. To define a global constant, it has to be used in the outermost scope:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |