PHP list(), Strings and Array Access

Array-Access on strings (not the SPL this time) goes that far in PHP when thrown against a list():

$string = 'AB';
list($char1, $char2) = $string;
echo $char1, ", ", $char2, "\n";;

That will output: “A, B”. Okay great. For more, imagine this then:

echo list($char1, $char2) = $string, ", ", $char1, ", ", $char2, "\n";

Which will output: “AB, A, B”. It’s only array access, but that list() construct actually still is evaluated as string, not array. That’s fine.

Keep in mind that this is array-like-access, so it only works on variables, and not on actual strings:

echo list($char1, $char2) = 'AB', ", ", $char1, ", ", $char2, "\n";

Output here: “AB, , ”

That’s really big love between constructs, notations and datatypes. Kisses in the moonlight. PHP version was 5.3.3 .

This entry was posted in PHP Development, Pressed and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s