Viewed   201 times

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):

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:

I am using PhpStorm 8.0.3 on MacOSX 10.9.5.

 Answers

4

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.

Sunday, October 9, 2022
 
3
<form name="add" method="post">
     <p>Age:</p>
     <select name="age">
        <option value="1_sre">23</option>
        <option value="2_sam">24</option>
        <option value="5_john">25</option>
     </select>
     <input type="submit" name="submit"/>
</form>

You will have the selected value in $_POST['age'], e.g. 1_sre. Then you will be able to split the value and get the 'stud_name'.

$stud = explode("_",$_POST['age']);
$stud_id = $stud[0];
$stud_name = $stud[1];
Tuesday, October 4, 2022
5

The usual workflow:

  1. Provide a Javascript rich-text editor for your users such as TinyMCE: http://tinymce.moxiecode.com/
  2. Grab the source generated by the RTE and filter it through HTML Purifier before saving to the database.
  3. Escape the existing HTML: <div id="myHtml" style="display: none"><?php echo htmlentities($html); ?></div>
  4. Re-populate the RTE via Javascript - in the case of TinyMCE as follows: tinyMCE.activeEditor.setContent($('#myHtml').html());

You can also load the HTML content via AJAX.

Tuesday, December 27, 2022
 
ghaag
 
3

There's no such thing as a PHP script. There's just files that happen to contain PHP code blocks. You can include() anything you want, but unless that included file has a <?php ... ?> block in it somewhere, the file's contents will simply be treated as output.

If css isn't being applied, then make sure that that your style sheets are actually being loaded, and that the HTML is being included in the proper place in your page's DOM.

Wednesday, August 31, 2022
 
5

In my opinion this really depends on how long the code snippet is. For simple lines I'd just use echo. For longer parts (especially with lots of " to escape) i'd prefer "closing" the php code (your second approach).

Depending on the amount of text/HTML to print I'd also consider using some kind of simple templating engine to load a template file and just fill in dynamic variables/placeholders/gaps.

Wednesday, October 12, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :