What Is Escaping In A String ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

What Is Escaping In A String ?

Post by UniqueIdeaMan »

Folks,

What does it really mean by "escaping" in a string ?

Eg.
https://www.tutorialspoint.com/php/php_strings.htm

The escape-sequence replacements are −

\n is replaced by the newline character
\r is replaced by the carriage-return character
\t is replaced by the tab character
\$ is replaced by the dollar sign itself ($)
\" is replaced by a single double-quote (")
\\ is replaced by a single backslash (\)

http://www.tizag.com/phpT/strings.php

Does it mean "translate"/"convert"/"substitute" or does it mean "ignore this single quote and don't take it as the ending single quote" ? I thought the former but some google result links mention the latter but mostly Googling brings irrelevant results. Brings up links related to mysqli_real_escape_string.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What Is Escaping In A String ?

Post by requinix »

Escaping means turning one or more characters with some special meaning and making them not have that special meaning anymore. The newline character is typically escaped by turning it into the string "\n" (backslash + 'n'), the tab character into "\t", etc. This is generally paired with unescaping, which is the opposite process: "\n" becomes a newline, "\t" becomes a tab, and so on.

' in PHP code can have a special meaning, depending where it's used. You escape it to prevent PHP from interpreting it with that special meaning. PHP then automatically unescapes it behind the scenes to result in the ' character.
Elli Sophia
Forum Newbie
Posts: 15
Joined: Mon May 08, 2017 10:35 am

Re: What Is Escaping In A String ?

Post by Elli Sophia »

As far as the knowledge that i have comes to play, escaping a string basically means to reduce ambiguity in quotes (and other characters) used in that string. For instance, when you're defining a string, you typically surround it in either double quotes or single quotes, 'Hello "World."' Or I can escape my quotes: "Hello \"World.\"".
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What Is Escaping In A String ?

Post by Christopher »

There are two kinds of escaping being discussed here. requinix and the OP are showing escape sequences which are character sequences that the parser converts to a different character. Examples of these are \n for newline or \t for tab. This is typically done for non printing or special characters. Elli Sophia gives the example of escaping a character that is not allowed or has a different meaning at that location. Examples are escaping a \' or \" within single and double quoted strings, or \$ so it is not considered the start of a variable name. In these cases the character that normally has a special meaning is shown literally. They are similar concepts but slightly different.
(#10850)
Post Reply