Hesam
Hesam

Reputation: 53610

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

Upvotes: 21

Views: 52796

Answers (5)

Workaround_Q
Workaround_Q

Reputation: 49

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

Upvotes: 2

Dhiraj Pandey
Dhiraj Pandey

Reputation: 181

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.

Upvotes: 9

acm
acm

Reputation: 6647

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>

Upvotes: 48

Erik
Erik

Reputation: 1106

Use

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

Or

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

Or

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

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382909

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.

Upvotes: 2

Related Questions