In many situations, the editor will automatically recognize a string as a language injection. In some cases, we may have to inject them manually.
For situations where the IDE can not automatically determine the language we're injecting in a string, we can tell it which language we're intending to use. Using Alt+Enter on the string contents, we can invoke the Inject Language/Reference intention and select the language contained in the string.
When manually injecting language references, it is possible that PhpStorm will only "remember" the injected language for a limited period of time. This period will depend on the language, context and the modifications that we make in other parts of our source code. If this happens, we can easily inject a language reference again (if the IDE doesn't recognize the language injection automatically).
Fragments of different languages can be edited inline, within the string literal, or in a separate editor tab.
Once the editor knows about the language injection, we can start working with it. Syntax and error highlighting and coding assistance are extended to this string.
We can also use the Edit <language> Fragment intention to open a separate editor tab in which we can work with the fragment.
In this separate editor tab, we can work with the source code in the corresponding language. PhpStorm will update the original string literal with the modifications we make.
A big advantage of working with separate editor tabs for language injections is that they will do escaping for us. For example, as we add a hyperlink in a string that used double quotes, the double quotes of our hyperlink will be escaped with backslashes in the original PHP document.
A very nice thing about language injections is how they behave when a database is connected to the project we are working in (see Databases and SQL Editor in PhpStorm). Not only will PhpStorm recognize SQL syntax and provide syntax highlighting and code completion on the injected language itself, but it will also know about tables and columns.
We can edit these queries in a separate editor by using the Edit MySQL Fragment intention. Since a database connection is configured, PhpStorm also lets us run this query immediately in the database console.
Some plugins, like the WordPress plugin, extend language injections. For example, when using the $wpdb->wp_posts variable (which references the wp_posts table in the WordPress database), the language injection editor tab will know about that variable and make the injected SQL statement more readable.