Sublime Forum

Issues commenting out <?php

#1

Hi all. I am experiencing unexpected behavior while commenting blocks, and want to figure out why it is occurring. In short, when I comment out a block of PHP code, it does not comment out the code as I expect it. This issue seems to only be present when the block of code I comment has opening or closing PHP tags in it, i.e. <?php* and/or *?> Here are two examples:

  • I created a simple PHP file with three lines of code:

    <?php echo "test"; ?>

If I select all three lines and click ctrl-/ to comment them, it becomes:

<!-- <?php
	echo "test";
?> -->

But the echo “test”; line is not greyed out as expected (screenshot)–why is that?

When I open this file in a browser, the echo “test”; line is indeed commented out.

  • When I comment more complex code, the behavior changes. If I start with the code:

    <?php add_action( 'init', 'process_post' ); function process_post() { echo "test"; } ?>

When I select all lines and click ctrl-/ to comment them, it becomes:

<!--
<?php
	add_action( 'init', 'process_post' );
	function process_post() {
		echo "test";
	}
?>
 -->

This time, the the <?php* and *?> elements are greyed out, but nothing else (screenshot)–why is that?

Most importantly though, when I open this file in a browser, none of the lines are commented out as expected–“test” is still echoed to the screen, and the source code appears as:

<!--
 -->
test

I’m not sure what’s happening here, nor how to resolve it. I just want to be able to select several lines of code, click ctrl-/, have them all commented out in my source code, and appear in grey text.

Thanks in advance.

0 Likes

How to easily comment out lines that are a mix of HTML and PHP?
#2

the important thing to remember is that the PHP is still evaluated on the server, even if it is inside a HTML comment. So it is correct that the echo "test"; line is not highlighted as a comment. The PHP preprocessor tags (<?php ?>) appear in the comment color because your color scheme does not target them, so they take the color from the parent, which is a HTML comment block. This open issue, once fixed, would prevent the HTML comment highlighting from propagating through to the PHP code:

again, it depends what your color scheme targets. Mine doesn’t target the variable.function scope, so the add_action appears in the comment color for me.

like I said, the PHP is still executed - if you want to comment the PHP code, put the comment inside the PHP tags:

<?php
/*
	add_action( 'init', 'process_post' );
	function process_post() {
		echo "test";
	}
*/
?>

and, btw, if you really want a keyboard shortcut to delete the current line (as opposed to comment it) as per the title of this thread, you can just press Ctrl+Shift+K

0 Likes

Commented code is still operating
#3

Hey thanks for your reply.

OK I understand what you’re saying about the syntax highlighting within Sublime, that there is an open ticket, and the current workaround (merely comment out the code inside the <?php and ?>).

But one issue is still unexplained as far as I see. In my first case, the resulting source code has the “test” inside an HTML comment:

<!-- test -->

But in my second case, the resulting source code has the “test” outside an HTML comment:

<!--  -->
test

Why the discrepancy?

and, btw, if you really want a keyboard shortcut to delete the current line (as opposed to comment it) as per the title of this thread, you can just press Ctrl+Shift+K

Whoops, that title was a typo. The title of my previous forum post was inserted and I didn’t catch it. I’ll change it now. Thanks for the shortcut reminder though–it’s a handy one.

0 Likes

#4

I guess you have a call to the process_post() method somewhere after the HTML comment ends

0 Likes

#5

OK good thinking–that’s probably the case here.

Thanks for clarifying all that! We can consider this resolved.

0 Likes

#6

So are we ever going to be able to comment out “<?php ?>” code from the outside?

0 Likes

#7

If you’re referring to having <!-- <?php echo "test"; ?> --> not execute the enclosed PHP, that’s simply how PHP works.

0 Likes