views:

399

answers:

4

In C you can continue a string literal in the next line escaping the newline character:

char* p = "hello \
new line.";
( My C is a bit rusty and this could be non 100% accurate )

But in php, the backslash is taking literally:

$p = "hello \
new line.";

I.E. the backslash character forms part of the string. Is there a way to get the C behavior in PHP in this case?

+6  A: 

Is it possible to simply concatenate your string like this:

$p = "hello " .
     "new line.";
JYelton
Your're right, I knew this... silly me.
Petruza
+2  A: 

In PHP you can simply continue on a new line with a string.

eg.

<?php
    $var = 'this is
    some text
    in a var';
?>
Constant M
$var will contain the newlines.
Vinko Vrsalovic
point taken, but when generating HTML, you need to have a <br /> tag to denote a new line. This is not the case in a <textarea> though.
Constant M
Yes but PHP's output is not always HTML.
Petruza
That's not what I said. Please read my response again. :)
Constant M
+2  A: 

The short answer is that you cannot do it as easily as in C, you need to either concatenate (best):

$p = "This is a long".
     " non-multiline string";

or remove the newlines afterwards (awful, don't do this):

$p = "This will contain the newlines
 before this line";

//for instance str_replace() can remove the newlines
Vinko Vrsalovic
+4  A: 

There's a few ways to do this in PHP that are similar, but no way to do it with a continuation terminator.

For starters, you can continue your string on the next line without using any particular character. The following is valid and legal in PHP.

$foo = 'hello there two line 
string';

$foo = 'hello there two line 
 string';

The second example should one of the drawbacks of this approach. Unless you left jusity the remaining lines, you're adding additional whitespace to your string.

The second approach is to use string concatenation

$foo = 'hell there two line'.
'string';

$foo = 'hell there two line'.
 'string';

Both example above will result in the string being created the same, in other words there's no additional whitespace. The trade-off here is you need to perform a string concatenation, which isn't free (although with PHP's mutable strings and modern hardware, you can get away with a lot of concatenation before you start noticing performance hits)

Finally there's the HEREDOC format. Similar to the first option, HEREDOC will allow you to break your strings over multiple lines as well.

$foo = <<<TEST
I can go to town between the the start and end TEST modifiers.  

 Wooooo Hoooo.  You can also drop $php_vars anywhere you'd like.

Oh yeah!
TEST;

You get the same problems of leading whitespace as you would with the first example, but some people find HEREDOC more readable.

Alan Storm
None of this will prevent the newlines being added (unless you include newlines when you mention whitespace)
Vinko Vrsalovic
Yeah, I meantally included newlines when I mentioned white-space. I deal mostly in HTML output so I forget that that newlines are a big deal in some areas. (note: the concatenation method I, and others, mentioned **will** prevent the newlines from being added.)
Alan Storm
Thanks for the long and thoughtful answer
Petruza