tags:

views:

43

answers:

2

I know I can create a verbatim string literal in C# by using the @ symbol. For example, the usual

String path = "C:\\MyDocs\\myText.txt";

can also be re-written as

String path = @"C:\MyDocs\myText.txt";

In this way, the string literal isn't cluttered with escape characters and makes it much more readable.

What I would like to know is whether PHP also has an equivalent or do I have to manually escape the string myself?

+4  A: 
$path = 'C:\MyDocs\myText.txt';

" double quotes allow for all sorts of special character sequences, ' single quotes are verbatim (there's only some fine print about escaping ' and escaping an escape \).

deceze
See also: http://php.net/manual/en/language.types.string.php
NullUserException
+1  A: 

Even single-quoted strings in PHP have the need for escaping at least literal single-quotes and literal backslashes:

$str = 'Single quotes won\'t help me \ avoid escapes or save a tree';

The only non-parsed solution for PHP is to use nowdocs. This requires you use PHP 5.3.

$str = <<<'EOD'
I mustn't quote verbatim text \
maybe in the version next
EOD;
Bill Karwin
You don't need to escape backslashes, unless they could be escaping a single quote.
deceze
Why so you're right! Mea culpa.
Bill Karwin