Language Injection in PhpStorm

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

This entry was posted in Cool Feature and tagged , , , , , . Bookmark the permalink.
  • Oink

    Wow, didn’t know this before. Thanks will use this in future.

  • Andreas Möller

    Very helpful feature!

  • William

    A cool feature that I use a lot. Unfortunately, I run into a lot of false positive errors with some languages, especially SQL. (“Select * from $db.$table” is invalid, but shouldn’t be, and the inspection that causes the error can’t be disabled.)

    It may also be helpful to know that heredoc strings can be automatically detected. <<< JS is always treated as Javascript, <<< HTML is HTML, << Language Injections.

  • Crantzbea

    How do I make the lightbulb go away in the code editor? I have tried disabling all inspections.

    • Andriy Bazanov

      It’s not possible to disable light bulb.

      But will be possible in v7.

  • Jan R

    try ALT+Enter -> “Replace quotes” with this example:



    (Sorry for double post because of html tags)

    but should be:


    • MadDocNC

      Well, it seems to be not so complicated function. It also transforms
      “$foo” to ‘$foo’ not ”.$foo.”

      • MadDocNC

        sorry,used tags…
        “foo $bar” to ‘foo $bar’ not to ‘foo ‘.$bar

  • Pingback: Syntax highlighting of PHP inside JavaScript (and other languages) | JetBrains PhpStorm Blog()

  • Pingback: 複数言語が入り交じったファイルのシンタックスハイライト(PHP内のJavaScriptなど) | JetBrains ブログ()

  • Lanre

    When using Edit HTML Fragment Webstorm insert \n in the src string for each line break. Is it a way to ask Webstorm to automatically visually format those string by indenting the code? Maybe by splitting the string and add indent? The problem is that the editor is very usefull but when looking at the src it’s a mess to read, we end up with super long string, visually unformated and afaik we need to reedit each string to actually see it nicely.

  • Pingback: Language Injections Enhancements in PhpStorm 8 | JetBrains PhpStorm Blog()