In my framework I can have javascript files with embedded php.
For example
$(document).ready(function(){
alert(<?php echo $message ?>);
});
How can I let PhpStorm detect the right languages so both javascript and php get correct syntax highlighting?
UPDATE 2023-04-24
As PhpStorm v9 (which is year 2015 or so), the IDE automatically detects the outer language if double file extension is used (the actual ticket: WI-24237).
So if your file is named some.js.php then it will automatically use JavaScript with PHP and there is no need for steps #2 and #3.
Three steps:
Give such a file custom (e.g. *.phpjs) or complex (*.js.php or *.php.js) file extension.
If the file extension does NOT end in .php then assign such a pattern to PHP file type in Settings/Preferences | File Types. This is needed so the file is treated as PHP (otherwise you will have no PHP syntax highlighting).
Settings/Preferences | Languages & Frameworks | Template Data Languages -- locate your file there and assign JavaScript to it (or whole folder, if all files in it will be treated similarly) -- this will set JavaScript as primary (outer) language instead of default HTML.

(PHP & JavaScript syntax colors + showing a tooltip for JavaScript's alert() function in a file named some.js.php)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With