PHP Quick Tip: Comment Block

PHP has 2 types of comments: comment block (for long comments in many continuous lines) and short comment (for one line). When test code, if we don’t want to a block of code execute, we can disable it by turning them into comment blocks using /* ... */, and when we want to execute it, just remove /* and */ characters at the beginning and the end of code. This action is often repeated, and in this article, I want to share a small tip to make this process faster and easier.

Let’s see an example: we have the following code:

function foo() {
    echo 'foo';
}

foo();

When we don’t want this code execute, we turn it into comment block like this:

/*
function foo() {
    echo 'foo';
}

foo();
*/

When we want to execute them, just remove /* and */.

OK, that’s used to be. Now, the trick is using //*/ for the last line of comment block, like this:

/*
function foo() {
    echo 'foo';
}

foo();
//*/    // this is the trick

Now, if you want to execute this code, just insert ONE SLASH / at the first line of block comment, like this:

//*    // insert "/" here
function foo() {
    echo 'foo';
}

foo();
//*/

You see, the last line of comment block is commented automatically, and by this way, only 2 lines (first and last) are commented, and the code itself will execute!

If you want to disable this code, simply remove ONE SLASH / at the first line of comment block. And all the code will be commented.

3 Comments

  1. I have been reading your posts regularly. I need to say that you are doing a fantastic job. Please keep up the great work.

Leave a Reply