Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of C#'s verbatim string literals in PHP?

Tags:

string

php

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?

like image 910
anonymous Avatar asked Aug 23 '10 23:08

anonymous


People also ask

Is there a replacement for C?

Some programmers consider popular languages like Rust, Go, D, and Carbon as C/C++ replacements. Meanwhile, some programmers consider using those languages as C/C++ alternatives that might replace C/C++ in the future.

What is the alternative to class for C?

You can swap "Class" in C++ for "struct".

Is C+ the same as C?

Syntax: C++ is an extended version of C, therefore both have a similar syntax, compilation and code structure. Keywords: Most of C's keywords and operators are used in C++ and perform the same function. Execution: C and C++ both follows top-down execution of the code.

Is there pass in C?

Parameters in C functions There are two ways to pass parameters in C: Pass by Value, Pass by Reference.


2 Answers

$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 \).

like image 196
deceze Avatar answered Sep 20 '22 03:09

deceze


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;
like image 32
Bill Karwin Avatar answered Sep 19 '22 03:09

Bill Karwin