-1

This question might be a duplicate of why in array single quotes not required according to the syntax

but I didn't the explanation I need over there. We know we can do echo "$myArr[0]", but we can't do the same with associative arrays like echo "myAssocArr['myKey']". I know we can use php complex syntax to get around, like echo "{myAssocArr['myKey']}".

My question is, if php can access normal index arrays without complex syntax, then why can't it access accosiative arrays' values as well. What is happening inside php compiler/zend/whatever engine that prevents it from accessing assosiative arrays' values in a string without the complex syntax.

Ermenegildo
  • 1,286
  • 1
  • 12
  • 19
user31782
  • 7,087
  • 14
  • 68
  • 143
  • I can use `echo "$assocArray[mykey]";`. – vee Nov 27 '21 at 18:31
  • @vee I did the same, but other posts on stackoverflow suggest it is a bad practice, because thats not standard php allowed syntax. – user31782 Nov 27 '21 at 18:39
  • @vee None of your links use array keys without quotes. Here it says its a bad practise and might throw error https://stackoverflow.com/a/13622685/3429430 – user31782 Nov 27 '21 at 18:50
  • It doesn't throw any error. tested on PHP 7.0+. https://www.php.net/language.types.string Please take a look at **example 15** `echo "He drank some $juices[koolaid1] juice.".PHP_EOL;` It's without quotes (`'`). The answer from KIKO also no quotes. – vee Nov 27 '21 at 18:52
  • But If this code `echo "$arr['mykey']";` <-- Yes, this will throw the errors. :) – vee Nov 27 '21 at 18:53
  • @vee Here on [php official page](https://www.php.net/manual/en/language.types.array.php#language.types.array.donts) it is considered bad practise, because if we have a predeined constant named `mykey` then `$assocArray[mykey]` will not take `mykey` as a key of the array, but a constant. I haven't checked may be it will throw error as well. – user31782 Nov 27 '21 at 19:00
  • Please copy this code and test it yourself. ```define('mykey', 'My key'); echo mykey; echo '
    '; $arr = [ 'mykey' => 'key in array.', ]; echo "$arr[mykey]";``` It is different with constant. What I get from `echo "$arr[mykey]";` is **key in array.** NOT **My key** from constant.
    – vee Nov 27 '21 at 19:02
  • It is completely different between `echo $arr[mykey];` and `echo "$arr[mykey]";`. The first one, if `mykey` constant is not defined it will be error undefined constant. But the second one will be no error if `$arr` array has `mykey` as the array key. – vee Nov 27 '21 at 19:08
  • What error you have got? I tested on PHP 8.0.8 but no errors. This is the link to full formatted code. https://onecompiler.com/php/3xjpud64q – vee Nov 27 '21 at 19:09
  • You are right. `echo "$arr[mykey]";` doesn't give error. Again, this is unexpected for me. Can you explain why? – user31782 Nov 27 '21 at 19:12
  • I can't explain anything about this, please continue reading on KIKO's answer he's know better than I am. – vee Nov 27 '21 at 19:13

1 Answers1

1

I read the manual on variable parsing.

I don't think there's any reasonable explanation other than that " and ' can be used to start and end a string. However, when you are parsing a string for variables you are already in a string. Those "tokens" are just not allowed in the simple syntax. Encountering a new begin or end of a string would make little sense. You can however use the syntax:

echo "myAssocArr[myKey]";

which avoids this problem. I agree it is not very satisfactory, but other than checking the PHP source code, I don't think you'll get a better answer.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • _" and ' can be used to start and end a string._ ... _Those "tokens" are just not allowed in the simple syntax._ They are, specifically single quote `'` is allowed. E.g. `echo "$var is a 'variable'"`. It is obvious for strings delimited with double quotes `"`, single quote `'` is not a start/end token. – user31782 Nov 27 '21 at 18:41
  • @user31782 I was referring to the "simple syntax" used when parsing variables, not to what is allowed inside strings. See the link at the beginning of my answer. – KIKO Software Nov 27 '21 at 18:43
  • Yes in a _simple syntax_ single quote `'` **cannot** be a start/end token. – user31782 Nov 27 '21 at 18:45
  • @user31782 I agree, but the algorithm used might not be aware of that. I made an assumption. – KIKO Software Nov 27 '21 at 18:47
  • I still think it's not a good guess, because along the same lines, `echo "$var is a 'variable'"` should throw an error as well. This is my main question, what's so special about arrays that an otherwise allowed single quote `'` is no longer allowed. – user31782 Nov 27 '21 at 18:53
  • @user31782 The PHP string parser is not in the same state, when parsing a string, then when it is parsing a variable. In the first state a single quote is allowed in a double quotes string, but when parsing for a variable, or array, after a `$` it doesn't allow a single quote. I'm not telling you anything new, this is actually what happens. – KIKO Software Nov 27 '21 at 18:55
  • _but when parsing for a variable, or array, after a $ it doesn't allow a single quote_ But in `echo "$var is a 'variable'"` it allows for single quotes. – user31782 Nov 27 '21 at 19:01
  • 1
    @user31782: You repeat the same point. In `echo "$var is a 'variable'"` the single quote is not part of a variable that needs to be parsed, but in echo `"$var['test'] is a variable"` it is. That is a different situation, which, as we know, is treated differently, and it has to because it is not a simple string, it is a variable inside a string. – KIKO Software Nov 27 '21 at 19:07
  • Ok, it does make some sense now. I would still appreciate if someone could link to a canonical reference or actual compiler code to back this claim up. They are different as you say, but I don't see any problem for `'` to be a part of a variable name. – user31782 Nov 27 '21 at 19:20