php escape char – PHP Escape Sequences

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?

Escape Sequence Example

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.
Read Also:  Laravel 6 orderBy for Multiple columns

escape sequences Example

    $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
// "Welcome Infinityknow"


$name = 'Infinityknow';
echo "Welcome {$name}"; // "Welcome Infinityknow"

Single-quoted strings (‘string’)

$name = 'Infinityknow';
echo 'Welcome $name'; // "Welcome $name"

$name = 'Infinityknow';
echo <<<'NOWDOC'
Welcome $name
// "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

Read Also:  Build Event Calendar with jQuery , PHP & MySQL

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 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.