Is it better to use a quoted string or defined constant in array references?

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?

define(‘_NAME’, ‘name’);
$myData[ _NAME];

Ask yourself what defining a constant does.

  • A constant’s value cannot be changed after it is set
  • Constants can be accessed regardless of scope

Is that what you need?

Not quite… I’m wondering more about performance (and maybe maintenance, but I don’t think that there is a difference from a maintenance standpoint)…

If I have 1000 references to the array in the code is A or B faster (or no diff):

A: $myData[ ‘name’];
B: $myData[ _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.

4 Likes

I don’t know if using a constant would be faster or use less memory than using a variable. I imagine any difference would at most be insignificant.

What I have used constants for is security and convenience.

For example, in a file outside of root I might have something like

define('MY_DB_PASSWORD', 'monkeypaw'); 
define('MY_HASH', 'dyhb3Abuw67dF2gsr952qkiDnyU571Aphutdn'); 

For the db password, using a constant means I can have script include the file and I don’t need to have the value in a file inside root.

For the hash, using the constant means I don’t need to remember what it is or risk typos.

2 Likes

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…

I prefer to create SQL statements using PHP HereDoc syntax and discovered that all constants and defined variables are not allowed.

I had to create temporary variables as a KLUDGE:

define(’_NAME’, ‘name’ );
define(’_NAME2’, $myData[‘name’] );

$var_001 = $myData['name'];
$var_002 = $myData[ _NAME];

// BEWARE:  ONLY linefeed allowed after ____TMP
$sql = <<< ____TMP
  SELECT 
    `*` 
  FROM
    `myTable`
  WHERE 
    `column_name` = "_NAME2"            # NOT ALLOWED 
    `column_name` = "$myData[_NAME]"    # NOT ALLOWED
    `column_name` = "{$myData[_NAME}]"  # NOT ALLOWED
    `column_name` = "$myData['name']"    # NOT ALLOWED
    `column_name` = "{$myData['name']}"  # ALLOWED
    `column_name` = "$var_001"           # ALLOWED - needs checking
    `column_name` = "$var_002"           # ALLOWED - needs checking
  ORDER BY
    `column_name` DESC
  ;
___TMP;
// BEWARE: ONLY linefeed allowed after ____TMP;

Indeed, it won’t work in heredoc, but it does work in nowdoc.
Basically nowdoc is to heredoc, as double quoted strings are to single quoted strings.

I personally dislike it, as well as heredoc and double quoted strings, if you want to you can.

Please supply a simple example of passing a defined CONST to nowdoc complete with output.

My searches found that the only way is to use an intermediary function such as sprintf(…).

Edit:

Any idea why it is that heredoc / nowdoc don’t support the use of constants? Maybe there is a logical reason, but it seems like a miss…

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 …

Indeed, it would appear const are not supported, but I mainly thought you were talking about variable interpolation.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.