7
$var = "a b // I want to comment here but it becomes a string instead c" 

I want to insert a comment in the middle of multiple line string in PHP, but I can't. I've tried /**/, //, and #.

Does anyone know how to do it?

12
  • you can't have, since string is a constant. Commented Apr 12, 2016 at 5:27
  • 3
    It's important. To point out where the error lies for an SQL query string for example. Commented Apr 12, 2016 at 5:28
  • 3
    so then you can use SQL comment in php string like this SELECT * FROM orders -- comment WHERE p < 25 or you may use # instead of -- Commented Apr 12, 2016 at 5:33
  • That's helpful. Should make it into a different question post sometime. Commented Apr 12, 2016 at 5:35
  • 1
    @kungphu Sometimes complex queries are helped by inline comments. I know I have a tricky regex with comments in the string itself (using the x modifier to allow it and generous whitespace) Commented Apr 12, 2016 at 13:02

5 Answers 5

11
$var = "a b ".// I want to comment here but it becomes a string instead." "c"; echo $var; 
Sign up to request clarification or add additional context in comments.

4 Comments

Ugh, that's not commenting within a string. Please at least clarify to the asker what you are doing, since as it is, it looks like you are promoting ".// as the "string commenting operator".
if u know the answer for this then paste it..we will also get knw the correct answer
The correct answer was given already. No need to be redundant. Why not edit your answer to make it better, instead of falling back to being miffed?
@SebastianMach Bit late, but I just submitted an edit that was six years overdue. So hopefully that’s better and gets approved
9

Only possible with concatenation and a comment:

$var = "a\n" . // "b\n" . "c"; 

2 Comments

This is not useful as its not printing "b". @Scopey
Ha sorry I misunderstood the question, I thought the "b" wanted to be commented out. Accepted answer is the way i'd do this.
2

you can also write your code like this. If you want multi line you can use \n at after string.

$string = 'First'; $string .= 'Comment section ';//this where you can comment $string .= 'Last'; 

2 Comments

Nice to know that. Finally, I know .= is possible.
Yes, @Aminah Nuraini you can achieve your task with concatenation
1
<?php $var = "a\n" . "b\n" . //this where you can comment "c"; echo $var; ?> 

Output

a b c 

Check code and output in editor. Click Here

Comments

0

You got your answer but I thought I'll share my 2 cents as well: You could use the HTML comment tag:

<!--This is a comment. Comments are not displayed in the browser--> 

The catch is that users can view this comment if they view source!

With the example you gave:

$var = "a b <!--I want to comment here but it becomes a string instead--> c"; 

And while we're at it, you can use /**/ inside Style tag(CSS):

.Class { width: 100%; /*The width*/ } 

Or as someone already suggested just close the string and comment inside PHP:

$var = "a". "b". //Comment "c"; 

And if you want to maintain the new line:

$var = "a\n" 

1 Comment

The first solution won't work if I the string as an SQL query

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.