Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment out HTML and PHP together #18244

Open
atiquz opened this issue Apr 4, 2025 · 6 comments
Open

Comment out HTML and PHP together #18244

atiquz opened this issue Apr 4, 2025 · 6 comments

Comments

@atiquz
Copy link

atiquz commented Apr 4, 2025

Description

 <!--               <span class="stamp stamp-md <?=$item['class'];?> text-white mr-3">-->

here you could see html is commented but not PHP so it is highly requested to create a new feature that will comment out HTML and PHP together.

@iluuu1994
Copy link
Member

Hi @atiquz. So, what should happen? Should the PHP not evaluate? That means we'll send PHP code over the wire, which is likely not a good idea.

@thomascorthals
Copy link

If you don't want to execute the PHP code and don't need the HTML output at all (i.e. it doesn't even have to be there for somebody looking at the HTML source), you can do it like this instead.

<?php /*<span class="stamp stamp-md <?=$item['class'];?> text-white mr-3">*/ ?>

@atiquz
Copy link
Author

atiquz commented Apr 9, 2025

@thomascorthals Thanks! Yes, I usually comment out sections of HTML + PHP like this:

<?php /*<span class="stamp stamp-md <?=$item['class'];?> text-white mr-3">*/ ?>

While this works, it feels verbose and a bit hacky, especially when working with mixed PHP/HTML template code (e.g., in .php, .tpl, or .phtml files). It’s easy to make mistakes with /*...*/, and these comments don't look clean or readable within an HTML structure.


✨ Proposal: Unified PHP + HTML Comment Syntax

Option 1: PHP-native comment block

<?# <span class="stamp stamp-md <?=$item['class'];?> text-white mr-3"> #?>
  • Simple and readable

  • Avoids execution of PHP (<?=$item['class'];?>)

  • Prevents HTML output

  • Great for commenting full PHP+HTML blocks

This would work inside PHP mode (<?php ?> or <?= ?>) and would behave as a short PHP-block-safe comment.


Option 2: HTML-style hybrid comment block

<!--# <span class="stamp stamp-md <?=$item['class'];?> text-white mr-3"> #-->
  • Even cleaner for HTML-heavy templates

  • Could be safely ignored by browsers like a regular HTML comment

  • Could be enhanced by PHP to skip execution of any embedded PHP inside it

  • Familiar format (builds on <!-- ... -->)

  • Works in HTML-first views or mixed templating engines (like Blade, Twig, or raw PHP templates)

This version could be processed by PHP (if supported in future versions) to skip output and skip executing embedded PHP inside the block. That would make it both safe and super developer-friendly.


✅ Advantages (for either syntax):

  • Prevent both HTML rendering and PHP execution

  • Cleaner and safer than <?php /* ... */ ?>.

  • Improves code clarity and avoids common comment mismatches

  • Helps in toggling or preserving sections for later debugging/experimentation

  • Useful in large dynamic templates with embedded logic

  • Can enhance tooling support (syntax highlighting, folding, IDE understanding)


Would love to hear your and others' thoughts. Do you think introducing a new HTML+PHP-safe comment syntax like <?# ... #?> or <!--# ... #--> would be a worthwhile addition for improving the developer experience when working in mixed templates?

@iluuu1994
Copy link
Member

For single lines, <?php # foo ?> already works. <? # foo ?> will work if you have short tags enabled. https://3v4l.org/eVUVM

I don't think <!--# is a good option because it would break existing HTML comments, which definitely exist.

https://sourcegraph.com/search?q=context:global+%3C%21--%23&patternType=keyword&sm=0

@thomascorthals
Copy link

I agree that it must start with <? if it's intended for PHP (non-)execution. <!-- is HTML markup that should be sent to the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants