I’m curious if there is any benefit to one or the other of using a quoted string vs. a defined constant when referencing an array element.
If I have this array: $myData[‘name’] and I need to have this array element referenced many times in a script. Should it simply be referenced everywhere as:
$myData[‘name’]
Or is it better to define a constant and reference it everywhere in code using the constant for the array element name?
Sounds like you are concerned with micro-optimizing. For readability $myData[ ‘name’] is quite clear what is being dealt with. The other one would have to be looked up to be sure what it is. Perhaps it is just an example, but if you have a thousand references to an array element you probably have a much bigger problem somewhere else.
I would suggest putting your project on a public repo for an overall peer review for those that may want to do that for you.
Yep, that’s some of the reasons I’m thinking about doing it. Plus, sometimes just for readability… If building something (like a sql statement) putting the quotes inside quotes inside quotes can get a little messy so I’m thinking doing something like the defined constant would clean that up a little.
It sounds like it’s a nitpik and just personal choice…
Because if I had a const named NormsIM, I couldn’t use that in nowdoc anymore (i.e., NormsIM said ...).
And since constants can be defined anywhere (not just in the same file) it would be quite the task to verify for each word you type in a nowdoc to verify it wasn’t defined as constant somewhere.
Keep in mind that even though the convention is to use ALL CAPS for constants, this is not a requirement, and all lower case is fine too. With that in mind, imagine I create constants of the words “the”, “a”, “is”, etc …