This is a really basic question for all you people out there who know PHP. This is not a problem but just something I'm confused about. I was reading this article about constants and wondered why are normal constants set with quotes around them, while "class constants" (those little things I learned yesterday from the article) don't have to have quotes around the constants when you define them? It seems really weird to me why PHP would do it this way?? Could someome please explain to me the reasons why there are 2 ways to define them - one via a function, and one via, what is it? A language construct? I don't know. The latter having no quotes around to define!! Really weird.
It's not really weird. Note that these constants are not exactly the same. Constants you've defined using the define() function, are accessible globally, in all scopes, while class constants are only accessibly in the class itself (or parent classes). define() is a function call, which doesn't necessarily expect a quoted string which you want to convert into a constant, as fist parameter. You can also use variables there, or even OTHER constants. Here an example: define('FOO', 'BAR'); define(FOO, 'test'); echo BAR; PHP: The above outputs "test". In the second define() call, we're not using quotes. This tells PHP to use the value from the constant called FOO. It DOES work without quotes too, but it's incorrect, because PHP searches FIRST for already defined constant to read it's value, and then it'll throw an E_NOTICE error because it's not defined.. and finally it'll convert the undefined constant (FOO) into a string. define(FOO, 'test'); // Does work, but leaves a E_NOTICE echo FOO; PHP: Now to the classes. As you see, it's another syntax, (no define() function is called), and here we're not able to use variables or constants after the const call. PHP will only expect an unquoted string, and throw a parse error if something else is found. To sum it up: You have to use quotes in define() because PHP will treat unquoted strings as constant first. Since you're trying to define a constant, it is undefined at this point, and PHP would throw an error. And in classes there's no point in using dynamic constant names. So that's why it allows you use stings without quotes. It's less work for the parser and therefore faster. However, this is not one the questions you should worry about too much at the beginning.