Language Injection in PhpStorm

Posted on by

In almost every project, we have a moment where we want to make use of language injection: embedding a small island of a different programming language in our code, typically embedded in a string. Consider embedding an HTML fragment in a PHP literal, a situation we’ve all no doubt have encountered. Here’s an example:

The $htmlFragment variable holds HTML code in which double quotes have to be escaped. If we were using single quotes, we’d have to concatenate strings and make our code even more unreadable. What if PhpStorm could help us out?

This functionality is available in IntelliJ Idea, PyCharm, WebStorm, PhpStorm, RubyMine, and AppCode.

Meet language injections. PhpStorm can recognize these situations and provides us with a full-featured HTML editor to edit the existing string literal:

We can also create a string and use the Inject Language intention to specify the string holds a different language:

A nifty little feature that is! And it also works in all other languages supported by PhpStorm. Let us know your thoughts in the comments below or in our forums!

Develop with pleasure!

– JetBrains PhpStorm Team