What Is Escaping In A String?

Folks,

What does it really mean by “escaping” in a string ?

Eg.

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.

The idea of ‘escaping’ in a string, is to prevent certain characters being used as a part of the coding language, and have them be represented correctly in the string itself.

3 Likes

In a string like myString = "he said "Stop it" and I kept doing it.", the code will be interpreted by matching all the adjacent pairs of double quotes. So there would be two strings "he said " and " and I kept doing it" with some stuff in between them.

One way of making sure this mis-interpretation doesn’t happen is to escape the inside double quotes like this: myString = "he said \"Stop it\" and I kept doing it." and the escaped quotes would be interpreted as actual quotes, not string delimiters.

Another way would be to just use single quotes like this: myString = 'he said "Stop it" and I kept doing it.'

2 Likes

Don’t worry. I know how to use double and single quotes and even know when to escape characters. I knew the spirit of escaping characters as it seems from you guys’ answers. I just didn’t know the full definition.
In short, escaping characters means just ignore the double quotes (mid ones throughout the string).

That’s one of the things that escaping characters can do, certainly, but only one of them. For example there’s no need to escape double-quotes, if your string assignment is surrounded by single quotes.

1 Like

Thanks guys!

I figured this out myself. Escaping means different things at different times. Sometimes it means translate something into another or convert or substitute (eg. convert /n to next line).
Sometimes it means do not take this/these single quote(s) as the closing single quote in a string (eg. echo ‘get lost you 'bird brain' dude’).
I figured all this out myself as tutorials weren’t that clear thus complicating things. I wanted an experienced person to confirm my guesses. And, it’s just been done!

I may be wrong but I can’t think of anywhere where escaping is converting. \n is not converting anything to anything. It is simply a way of representing a newline character.

That is what I meant by converting or translating. Meaning don’t take that /n literally but translate it (or understand it) as a command to make a new line.

I seldom use the backslash escape key except for when a HTML page is generated using PHP and the \n is useful for adding linefeeds to the HTML source.

I also prefer to using PHP HereDoc rather than using PHP strings. Far easier as you can see from the following example.

After selecting a different URL line parameter, view the HTML page source and notice the difference.

<?php 
	declare(strict_types=1); // PHP 7 specific
	error_reporting(-1);
	ini_set('display_errors', 'true');

$title = 'HereDoc Examples';
$style = 'style="background-color: #eef; padding: 1em; border: dotted 2px red;"';

$jbString = <<< ____TMP
	<br>
	<p $style>
		John_Betong's "Example" of using single and double quotes
	</p>	
____TMP;

$hdr = <<< ____TMP
<!DOCTYPE HTML>
<html>
<head lang="en">
<title> $title </title>
<style>
body {background-color: #f3f3f3; color: #333;}
li   {display: inline-block; padding: 0.88em 1.42em; background-color: lightgray;}
li:hover  {background-color: cyan;}
.bd1 {border:solid 1px #999}
.bgs {background-color:snow;}
.fwb {font-weight:700;}
.mga {margin: 1em auto;}
.ooo {padding:0 margin:0;}
.p42 {padding: 0.42em;}
.tac {text-align:center;}
.w88 {width:88%;}
</style>
</head>
____TMP;
echo $hdr;

echo '<body>';
	echo '<h1>' .$title .'</h1>';

	echo '<div class="w88 mga bgs p42 bd1">';
		echo $jbString;

		echo '<ul class="fwb tac">';
			for( $i2=0; $i2<6; $i2++):
				echo '<li> <a href="?line=' .$i2 .'">' .$i2 .'</a> </li>';
				echo "\n";
			endfor;
		echo '</ul>';

		$lineFeeds = [
			0 => '',
			1 => '<br>',
			2 => '<br>\n',
			3 => "\n",
			4 => "<br>",
			5 => "<br>\n"
		];


		$line = isset($_GET['line']) ? $_GET['line'] : 3;
		$lf   = $lineFeeds[ "$line" ];
		for( $i2=0; $i2<10; $i2++):
			echo ''
						.	'$lf => ' 
						.	 $lf 
						.	"&nbsp;  ==> " 
						.	$i2 ;
		endfor;
		echo '<h2 class="ooo"> Notice the difference in the page view source HTNL</h2>';

		$src = highlight_file( __FILE__, true );
		echo $tmp = <<< ________TMP
			<pre $style>
				$src
			</pre>
________TMP;

echo '<body></html>';

Edit:
Removed array element.

1 Like

Well done!
I noted your code on my note pad++ note. Will use it as a reference when looking back into heredoc tutorial.
:weee:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.