Is the behaviour of associative array keys in php 8.0.2 different to php 7? Should I enclose array keys in quotes, or not?

Scott Merewether Rankin

Can anyone help me to understand what is happening here?
How do the double and single quotes affect the behaviour of the associative array keys?
Is this behaviour different to php 7, why?

      <?php
        // php version 8.0.2
        echo "$assoc_array[key]";   // OK
        echo  $assoc_array[key];    // error undefined constant "key"
        echo  $assoc_array['key'];  // OK
        echo "$assoc_array['key']"; // error - unexpected string content ""
        // this behaviour seems inconsistent
      ?>

This is my first question put to the forum. Please be gentle.

Steven

Let's take a look at each line...

echo  $assoc_array[key];

Anything string that isn't preceded with certain characters (e.g. $|->), terminated with certain characters (e.g. (), or inside of quotes (or heredoc etc.) is assumed to be a constant.

Which means key is usually assumed to be a constant. But you haven't set a constant value for key and therefore PHP has to do something with it...

Before PHP 8.x that would mean assume it was supposed to be a string and try the look up again; this would generate a warning.

This behaviour changed in PHP 8.x and now it throws an Error. Which, if not handled, ends the script.

echo "$assoc_array[key]";

In this case key is enclosed in quotes and thus, treated as a string. Arguably I'd suggest that this should throw an error as well; it would seem more consistent.

However, it's the same behaviour across PHP 7 & 8.

echo  $assoc_array['key'];

This is just the standard way to access an associative array; single or double quotes would be fine. Again, this behaviour is consistent across PHP versions.

echo "$assoc_array['key']";

This one's a bit trickier. What it comes down to is that using double quotes and variables is a tricky business. PHP has to try and decide what parts are meant literally and what parts are identifiers and the current process is that quotes around keys is not allowed.

This is consistent across PHP versions.


Effectively PHP provides two ways of defining variables inside of double quotes:

simple

This means a variable preceded with a $. Which can be used like:

echo "$variable";
echo "$array[key]";

Complex

This means almost any variable you can think of in PHP. Which is used like:

echo "{$variable}";
echo "${variable}
echo "{$array['key']}";
echo "${array['key']}";
echo "{$array["key"]}";
echo "${array["key"]}";
echo "{$object->property}";

See the PHP 7 >> 8 migration manual for further information on the backwards compatibility:

https://www.php.net/manual/en/migration80.incompatible.php

N.B. The key difference, for your purposes, is that the way undefined constants is handled has changed (as above) from a Warning to an Error Exception

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Php, how to swap two associative array KEYs?

From Dev

How to assign new keys to a PHP associative array

From Dev

Strange associative array behaviour in PHP

From Dev

How to convert a PHP array to an associative array, using index as keys?

From Dev

PHP: How to extract() values from an associative array with hyphens/dashes in their keys?

From Dev

Merging values of duplicate names (keys) in associative array (PHP)

From Dev

PHP - Adding variables to an associative array with the variable names as keys

From Dev

PHP Get values from associative array based on list of matched keys

From Dev

PHP associative array with static vars keys or values ERROR

From Dev

PHP: How to extract() values from an associative array with hyphens/dashes in their keys?

From Dev

Merging values of duplicate names (keys) in associative array (PHP)

From Dev

Need help in php associative array for replacing keys and value

From Dev

Php multidimenson array sort (different keys)

From Dev

Display foreach for different keys in an array in PHP

From Dev

Php search same values in different array keys

From Dev

PHP array merge with different type of keys

From Dev

php associative array_search odd behaviour

From Dev

add keys to php array

From Dev

PHP remove array keys

From Dev

php, change keys in an array?

From Dev

Combine Array Keys PHP

From Dev

PHP array set keys

From Dev

PHP array merge with keys

From Dev

php array searching with keys

From Dev

Filter associative array with keys array

From Dev

Convert array of keys to associative array

From Dev

Modify array keys of an associative array

From Dev

PHP - merging 2D array by keys

From Dev

Split associative array based on keys

Related Related

HotTag

Archive