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

2

5 Answers

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.

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.

1

Use

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

Or

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

Or

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

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> <?p hp require($INC_DIR. "unneeded.php"); ?> </div>--> 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like