简体   繁体   中英

How can I comment out PHP lines inside HTML file?

In the middle my HTML code, I have a line of PHP. now, I want to make PHP line as a comment. I tried to ues <!-- --> but it seems not work for PHP.

What should I do?

Thanks

Imagine you have the following code:

<body>
    <?php echo $this_variable_will_echo_a_div; ?>
</body>

If you want the div to be echoed but not displayed at the page, you'll comment html , PHP still gets executed:

<body>
    <!-- <?php echo $this_variable_will_echo_a_div; ?> -->
</body>

If you don't want the div to appear at the source as commented html, you'll have to comment php , nothing will appear between the body tags at your source:

<body>
    <?php /* echo $this_variable_will_echo_a_div; */ ?>
</body>

All the commenting methods of PHP syntax works in the embedded code inside HTML. Feel free to use anyone.

<?php //for one line comment ?>

<?php /* for multi-lines comment */ ?>

also you can use the HTML comment syntax just outside the php tags.

<!-- <?php blah blah ?> --> 

Note that the PHP code will still be executed.

Use

<?php
/*
    <?php
        php code.. blah blah
    ?>
*/
?>

Or

<?php
    // <?php echo 'hi'; ?>
?>

Or

<?php
    # <?php echo 'hello'; ?>
?>

You need to use PHP Comments not HTML comments <!-- -->

Note that you should hide PHP code for security reasons when commenting out a chunk of HTML containing PHP code using <!-- --> otherwise your source code will be visible when page is viewed.

Comment out whatever code you wish with the usual HTML comment tags. Put a space between the p and h of php. For historical purposes, add an additional comment for any other code author who may do any editing in the future. *Note: Recommended for temporary use only when the commented out code will eventually be deleted. (I used it while waiting for approval from a client.)

 <!--Nullified php call with spaces between the letters p and h within commented-out html code -->
 <!-- <div class="foo">
                <?p hp require($INC_DIR. "unneeded.php"); ?>
            </div>-->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM