2

I'm using PHP 8.2. I have the following heredoc:

$a = 4; 
$b = 2;

$str = <<<EOT
$a + $b = {$a + $b}
EOT;

print $str;

Error:

PHP Parse error: syntax error, unexpected token "+", expecting "->" or "?->" or "["

The error is happening on the + sign inside the curly-braces {$a + $b}. I'm reading that complex variable interpolation is a bit different in PHP 8.2, but I cannot find any examples of how to do math with PHP variables inside a heredoc.

What am I missing?

1 Answer 1

1

Advanced string interpolation allows to execute arbitrary code in substitutions, like calling member functions, e.g., ${$myObject->someFunction()}. So you could move the logic into functions and use those. That would also make them easy to test.

Looks like there was a RFC to extend string interpolations but it didn't go anywhere.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your post. It pointed me in the right direction to -- begin understanding this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.