PHP Escape Sequence
Escape sequences are used for escaping a character during the string parsing. It is also used to represent line breaks, tabs, alerts, and more. The escape sequences are interpolated into strings enclosed by double quotes or heredac syntax.
Escape sequences are started with the escaping character backslash (\) followed by the character which may be an alphanumeric or a special character. If it is an alphanumeric character, it gives special meaning to represent the line breaks \n, carriage return \r, etc..
If it is a special character, then it will be considered as it is during the string parsing.
Strings in PHP can be specified in four different ways:
Single Quoted, Double Quoted, Heredoc Syntax and nowdac syntax.
The single quoted and double quoted are the most frequently used.
Single Quoted
The simplest way to specify a string is to enclose it in single quotes – ‘’. It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written. All the escape sequences like \r or \n, will be output as specified instead of having any special meaning. Single quotes are usually faster in some cases. The special case is that if you to display a literal single-quote, escape it with a backslash(\) and if you want to display a backslash, you can escape it with another backslash(\\).
Example:
<?php // A Simple String echo ‘I am a developer.’; echo “\n”; //use of \ to display string Echo ‘it will be interesting to know about the string.’; Echo “\n”; //to escape the backslash within the string Echo ‘a \\ is named as backslash’; Echo “\n”; ?>
Output :
I am a developer. it will be interesting to know about the string. a \ is named as backslash
Double Quoted
It is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the escape sequences. Each variable will be replaced by its value.
Example:
<?php // A Simple String echo “I am a developer.”; echo “\n”; Echo “it will be interesting to know about the string.”; Echo “\n”; //variables are included directly $string = ‘abc’; echo “the word is $string”; ?>
Output :
I am a developer. it will be interesting to know about the string. the word is abc
Heredoc
A third way to delimit string is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows and then the same identifier again to close the quotation.
The closing identifier must begin in the first column of the line. The identifier must follow the same naming rules as any other label in php.
Example –
<?php echo <<<EOT Hello World. EOT;
Output –
Hello World.
Nowdoc
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding php code or other large blocks of text without the need for escaping.
Example –
echo <<<’END_OF_HTML’ $hello this is {$a->test} END_OF_HTML;
Output –
$hello this is {$a->test}
How do you escape a single quote?
When using single quotes (‘’) to create a string literal, the single quote character needs to be escaped using a backslash(\’).
Widely used Escape Sequences in PHP
\’ – To escape ‘ within single quoted string
\” – To escape “ within double quoted string
\\ – To escape the backslash
\$ -To escape $
\n -To add line breaks between string
\t – To add tab space
\r – For carriage return
How to escape a single quote in mysql?
What is the difference between single and double quotation marks in php?
Double quoted strings: By using double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the escape sequences.
What is PHP Addslashes?
The addslashes() function returns a string with backslash in front of predefined characters. The predefined characters are: single quote(‘) double quote(“)
Conclusion –
In general, you use either or but you should be consistent with what type you use and when. For example you might choose to use single quotes by default unless you need to use variables or special characters within the string.
You might think that because double quotes give you more features, that it would be better to use them all the time, but single quotes are probably better for simple strings because you don’t need to escape special characters like $ sign.
Make your web application more responsive and intelligent by hiring top-notch PHP developers from Agira technologies.