PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
It is currently Mon Jan 21, 2019 4:16 pm

All times are UTC - 5 hours

Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Apr 26, 2017 5:28 pm 
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 197

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


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 (\)

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.

PostPosted: Thu Apr 27, 2017 2:46 am 
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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.

PostPosted: Thu May 11, 2017 5:27 am 
Forum Newbie

Joined: Mon May 08, 2017 10:35 am
Posts: 15
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.\"".

PostPosted: Thu May 11, 2017 10:59 pm 
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13587
Location: New York, NY, US
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.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 7 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group