php escape char \’ – 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 and \r – For carriage return.
php escape char – How To Use Escape Characters in PHP?
Contents
Escape Sequence Example
<?php echo "PHP Escape Sequences:\n Backslash \\ is used as an escaping character."; ?>
Widely used Escape Sequences in PHP
The valid escape sequences in PHP are shown in the following List.
- \’ – 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.
escape sequences Example
<?php $LatestString = "It is an \"escaped\" string"; $LatestSingleString = 'It \'will\' work'; $LatestNonVariable = "I have \$zilch in my pocket"; $LatestNewline = "It ends with a line return\n"; $LatestFile = "c:\\windows\\system32\\myfile.txt"; ?>
Double Quotes and Heredoc
$name = 'Infinityknow'; echo "Welcome $name"; // "Welcome Infinityknow"
$name = 'Infinityknow'; echo <<<HEREDOC Welcome $name HEREDOC; // "Welcome Infinityknow"
Alternately
$name = 'Infinityknow'; echo "Welcome {$name}"; // "Welcome Infinityknow"
Single-quoted strings (‘string’)
$name = 'Infinityknow'; echo 'Welcome $name'; // "Welcome $name"
$name = 'Infinityknow'; echo <<<'NOWDOC' Welcome $name NOWDOC; // "Welcome $name"
Character Escaping
$name = 'Infinityknow'; echo "Welcome \$name"; // "Welcome $name"
$name = 'Infinityknow'; echo "Welcome \\$name"; // "Welcome \Infinityknow"
Tab Characters: \t and \v
echo "Welcome\tPakainfo"; //results : Welcome Pakainfo
New Lines: \r and \n
echo "Left\nLeft\nRight\nRight";
Escape character: \e
echo "\e[32mPakainfo website\e[0m \e[33mInfinityknow website\e[0";
Don’t Miss : PHP Addslashes Function With Examples
I hope you get an idea about php escape char.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.