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?
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.
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
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.
You get the same problems of leading whitespace as you would with the first example, but some people find HEREDOC more readable.