PHP heredocs could be better

... but they can do weird stuff if you really want them to.

PHP heredocs aren't something I think most PHP devs (including myself) use day to day.
But did you know they have a secret power in editors like VSCode? Sub syntax highlighting.

Say you write some SQL for a report, rather than wrapping it in single or double quotes, try with heredocs with the identifier named SQL and you'll get SQL syntax highlighting inside:

The same is true for HTML:

Variable interpolation works here too:

The above example isn't secure though, it'd be open to an XSS attack. But as we can have variables reference anonymous functions, we can do things like:

Falling further down the rabbit hole.

We can't put foreach loops in for example, nor functions directly like arraymap but like the above example referencing the example escape attr method, we can have a variable which calls both arraymap and implode like so:

If you have Collections or similar you can do this without the helper:

Heck, if you want you can throw another Heredoc inside it!
Though at least at the point of writing, this breaks the syntax highlighting in VSCode.

The descent into madness begins...

At this point, you might ask, what else can we hack together here?

If statements? One way could be to have a function again which takes in a condition, true case and false case. But a simpler option is the ternary operator:

Switch (or rather, match statements), just like above:

And if you want something especially weird, there's a parser hack that will let you run essentially any PHP on the right side of this assignment like so:

Though this will trigger a deprecated warning in php-8.2+

Try any of these examples locally or over on https://onlinephp.io/

They could be better

I'm not suggesting you use these in your next project. Simple heredoc usage is great but these examples can be solved in much better ways such as the use of abstracted template files for rendering.

However, it'd be interesting for simple use cases if you could call functions or php directly without these hacks in heredocs.

There's a couple RFC's that attempted to bring this to PHP:
PHP RFC: Arbitrary Expression Interpolation
PHP RFC: Arbitrary string interpolation
Both appear to be withdrawn and had their drawbacks but maybe there's still chance we could get something similar in the future.

As PHP has deprecated ${} string interpolation maybe this syntax could be brought back but to allow calling PHP directly. Maybe to limit it's impact it only affects heredocs?
I don't think the presence of this feature would cause developers to no longer use better solutions when needed, but it would be handy for quick things where abstracting to a template is unnecessary.

Here's an example class which tracks logs and needs a simple way to render them as a list. I've added multiple renderLogs() methods showing a few possible solutions and well as the #{...} style for the possible proposed solution.

This isn't a perfect example, other examples could be where you want to render a total of 2 values and you don't want to break up the string just to add 2 numbers together. It doesn't have to be HTML of course either, maybe it's outputting to the terminal or sending an SMS, where adding template files perhaps wouldn't make as much sense.

Template Literals for example allow for this in Javascript. String interpolation in Ruby also allows for it, and they too use the #{...} style solution and similar solutions exist in other languages.

You can definitely have a view on your preferred method here, for many cases my go to would be either the foreach method or a view() style template function for now.
At the least I hope this helps demonstrate how we could interact with such a solution if it were to be added in the future.

Thank you for reading.

comments powered by Disqus
Want to setup your own server? Digital Ocean offer $100 free for new accounts.
DigitalOcean Referral Badge