简体   繁体   中英

Commenting out other comments with PHP?

Can I comment out lines which have other comments?

/*
 * comment 1
 */

$var = 0;
$if();

/*
 * comment 2
 */
$var2 = 2;

Is there a way to comment out all these lines together? I often have a long function or logic and would like to comment out the rest for testing.

Have you considered skipping over the code by placing it inside an if(0) block?

For example:

<?php
if( 0 ) {
     print("This code is 'commented' out");


... other commented out code is here ...


}
?>

From the PHP manual:

'C' style comments end at the first */ encountered. Make sure you don't nest 'C' style comments. It is easy to make this mistake if you are trying to comment out a large block of code.

 <?php /* echo 'This is a test'; /* This comment will cause a problem */ */ ?> 

You might be able to do something with Heredoc syntax, such as:

<<<VAR    
    /*
     * comment 1
     */

    $var = 0;
    $if();

    /*
     * comment 2
     */
    $var2 = 2;
VAR;

No. Your best bet would be to use the comment blocks /* */ sparingly, and use more line comments // or # .

A comment block can hide multiple lines, even if those lines have single line comments. However, a comment block cannot work with another comment block inside it.

Syntax here: http://php.net/manual/en/language.basic-syntax.comments.php

Most of code editors allow you to comment and uncomment whole blocks of code with // . Usually it is some combination of Ctrl+Shift/Alt+C . Very convinient Check your text editor's reference.

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