Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Interpolated String Dereferencing

It was not possible to use a literal string as a variable for an array, or a class name, and access, respectively, index, properties, constant or methods. It was not possible for interpolated strings, which are strings that include another string.

In PHP 8, this is now possible.

PHP code

<?php

$bar = "abc";

echo "foo$bar"[0];
echo PHP_EOL
echo "foo$bar"::foo();

class fooabc{
    static function foo() {
        print __METHOD__;
    }
}

?>

Before

PHP Parse error:  syntax error, unexpected '[', expecting ';' or ',' 

After

f
fooabc::foo

PHP version change

This behavior changed in 8.0.

See Also

Error Messages

Analyzer

© 2023-2026, Damien Seguy - Exakat