Home > Tutorials > PHP Tutorials > Escaping Characters and Variable Expansion

Escaping Characters and Variable Expansion

Escaping characters is basic, but essential to an error-free PHP script. Consider the following sentence for example:

Mike’s uncle said, “Isn’t that cool?”

Now if we attempt to use PHP to echo this sentence to the browser, those quotes and apostrophes will cause some problems. The quotes and apostrophes inside the string are confused for the symbols used to signify the end of a string. The backslash (\) is used to “escape” characters that might have a special meaning to PHP if interpreted by the web server. A backslash before a character causes it to be treated just as a literal character, not as any special symbol. So, let’s go back and see how we could echo out that previous sentence.

<?php  
 echo "Mike\\ \'s uncle said, \"Isn\\ \'t that cool?\"": 
?>

Now only the first and last quotation marks will be interpreted as meaningful to PHP; the others are just characters as far as it is concerned. Below is a list of special characters that can be denoted with the backslash escape character.

Escape Sequence   Meaning
\\’   Apostrophe
\\”   Quotation Mark
\\   Backslash
\\$   Dollar Sign
\\n   Newline
\\r   Carriage Return
\\t   Tab

Now there are a few things to take note of here. For starters, let’s take the following bit of code:

<?php  
 $applicant= "Mike";  
 echo "Hello $applicant": 
?>

PHP will replace $applicant with the variables value (“Mike”) – so instead of printing out Hello $applicant, it will print Hello Mike. This rather convenient replacement is variable expansion. However there may be times when you don’t want to do this. Maybe you want to actually use the name of a variable and echo that, rather than it’s value. We can accomplish that by simply escaping the $ before the variable name:

<?php  
 $applicant= "Mike";  
 echo "The \\$applicant variable is set to $applicant";  
?>

The first occurrence of $applicant is escaped, so PHP literally prints out $applicant. The second occurrence is not escaped, so PHP replaces it with the variables value of Mike. You can also suppress variable expansion by using single quotes instead of double quotes around a string. For example:

<?php  
 $name = "Mike";  
 echo "$name": // Will print: Mike  
 echo '$name'; // Will print: $name 
?>

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

5 Must Have WordPress Optimization Plugins For A Fast Loading Website

Having a fast loading website is essential these days. First off, users are impatient. If ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close