php Array Index Key with and without quotation marks difference

  • 2021-07-13 04:31:50
  • OfStack

Today, while reading an PHP blog, I noticed such a sentence: "If the index KEY value in PHP is not enclosed in quotation marks, the index KEY value will be interpreted as a constant, and when the definition of this constant cannot be found, it will be interpreted as a string". I don't quite believe it, because I always enclose the index KEY in quotation marks, so this doesn't happen. Take advantage of the time to write a line of code test 1:


define('WEBHOST','blog'); $wso = array(); $wso['blog'] = 'www.weixiaodeyu.com'; $wso['WEBHOST'] = 'www.ofstack.com'; echo $wso[WEBHOST];  //output www.weixiaodeyu.com

Sure enough, the output turned out to be www. weixiaodeyu. com instead of www. ofstack. com. Now you know the importance of quoting the index KEY value. If there is an unexpected error, you should check it for 1 time. In fact, if the language specification is strictly enforced, this kind of error that is difficult to debug will not occur.


Related articles: