Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm not highlighting PHP code inside a .php file when mixed with HTML

Tags:

html

php

phpstorm

I am having a problem in PHPStorm where it won't highlight PHP code (but will intellisense it) inside a .php file when it's mixed with HTML code.

Screenshot (click to enlarge):

Screenshot

If I invalidate cache/restart, it will, for a few seconds, show the correct highlighting. However, after it "calculates", it will return to not highlighting it.

Is there a setting for this? Or is it just a plain bug?

I have read about language injections, but this does not seem like the place for it since it's a .php file already and PHP is not on the list of injectable languages.

Relevant code:

<?php
$works = "yes";
?>

<html>
<body>

<script>
    var shouldStillWorkAfterThis = true;
</script>

<?php
$works = "yes";
?>

<table>
    <tr>
        <td><?php $works = "yes"; ?></td>
    </tr>
</table>

<!-- lets try split syntax -->
<?php
if ($works) {
    ?>
    <table>
        <tr>
            <td><?= $works ?></td>
        </tr>
    </table>
    <?php $thisShouldToo = true; ?>
<?php } ?>

<!-- lets break it -->
<div id="someclass">
    <header>
        <div class="someotherclass">
            <div class="andanextraclass">
                <!-- include a file-->
                <?php include('somefile.php');?>
                <?php
                    $anythingHereIsNowBroken = true;
                ?>
            </div>
        </div>
    </header>
</div>

</body>
</html>

Language Injections Settings:

Language Injections

I am using PhpStorm 8.0.3 on MacOSX 10.9.5.

like image 946
Joao Carlos Avatar asked May 30 '15 08:05

Joao Carlos


People also ask

Can you mix HTML and PHP?

PHP code is normally mixed with HTML tags. PHP is an embedded language, meaning that you can jump between raw HTML code and PHP without sacrificing readability. In order to embed PHP code with HTML, the PHP must be set apart using PHP start and end tags.

How do I highlight in PHP?

The highlight_string() function outputs a string with the PHP syntax highlighted. The string is highlighted by using HTML tags. The colors used for syntax highlighting can be set in the php. ini file or with the ini_set() function.


1 Answers

Based on your screenshot of Settings (Preferences on Mac) | Editor | Language Injections.

Please delete 3rd language injection rule from the bottom (the one for "div" -- that has "IDE" in Scope column).

That rule injects HTML into div tag which tells IDE to treat all other code (even PHP) inside such tag as HTML/plain text.

like image 109
LazyOne Avatar answered Sep 24 '22 12:09

LazyOne