4

I have a line of PHP source code I would like to keep in a PHP file as a comment, so it can be used when necessary. This is only a debugging script, so keeping the file clean is not an issue.

Nevertheless, I am trying to comment these lines using /* and */:

/* $path = FOLDER . "*/*/*/*/*.gif"; $files = glob($path); */ 

But this result in a parse error, because the path */*/*/ closes the comment block. An opening /* won't be treated as an opening comment inside a string, but since the commented code is not parsed, the */ is treated as a closing comment.

Can anyone can think of a workaround without using //?

5 Answers 5

5

use "*/*/*/*/*.gif" slashes as DIRECTORY_SEPARATOR constant?

$path = FOLDER . '*' . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . '.gif'; 

You can make a shortcut like define('DS', DIRECTORY_SEPARATOR);

Sign up to request clarification or add additional context in comments.

Comments

1

Just use a backslash \ before ever forwardslash /. It won't not affect the file path but will make the lines block commentable.

1 Comment

No. The backslashes will be included in the final string.
1

You will have to use // or a method like message or bluegman991 suggested.

'C' style comments end at the first */ encountered. 1

The interpreter terminates the comment at the first */ regardless of whether it is part of a string or nested comment.

Your IDE may have a shortcut to toggle // comments on blocks of code. For example, Eclipse uses Ctrl + Shift + C to toggle comments on blocks of code.

5 Comments

I deleted my answer of using goto for fear of being downvoted back to a score of 1. However, it is the only way to bypass blocks of code that use multi-line comments. For this particular question, I think it's best to just rewrite the string. +1 for using IDE to apply the single line comments.
@webbiedave Clever answer IMO but I understand your fears. Anti-goto indoctrination has been strong for longer than I've been programming :)
@drew010 I saw your solution I think it was perfectly valid. The only drawback I could see is that it would not be highlighted as a commented section in my IDE (eclipse) and would be easier to forget. But I understand your fear :)
(I got confused my comment was intended for @webbiedave)
I figured that :) However, my solution was just for use during a debugging session, not for comments that will remain in source.
0

for any special character you wanna use inside a string you can use \ without affecting on the behaviour of this string

Comments

0

I personally often use:

if (false) { ...code to be skipped... } 

This way you should avoid the problem, but missing the highlighting from your ide (unless it is a very very very smart ide :))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.