Here’s an example. We’re generating an HTML e-mail body here in which we’re using several PHP variables and so forth. PhpStorm recognizes that the string holds HTML mixed with PHP and provides syntax highlighting, code completion and navigation. Yes, all inside a string.
But there’s more! Let’s look inside.
In the above examples, PhpStorm was able to recognize the fragment’s language. In other cases, it will not. We can easily hint the language injection by using the Inject Language/Reference intention (Alt+Enter).
Once the IDE knows the language injected, we can use the Edit Fragment intention (again, Alt+Enter) to open up a separate editor in which we can edit the language fragment. The advantage is that we can focus on the contents of the string instead of things like escaping, or adding the text we’re typing to the correct concatenated string part. PhpStorm will do all that for us.
Language injections work on any type of string: regular strings, concatenated strings, HEREDOC, and so on. If a string contains a code fragment that does not belong to the language being edited, PhpStorm will simply gray it out so its original value is always preserved. The last thing we want to do is break your precious code!
Speaking of SQL… Depending on the context, PhpStorm can figure out the value of a PHP variable in a SQL language injection. For example, the $wpdb->posts variable below will be materialized into a known table name when editing the fragment.
This also means that running this query in the console (using the intention) will only prompt for one variable, $id in this case, since the IDE already knows the table name.
Develop with pleasure!
– JetBrains PhpStorm Team