Thursday 11 March 2021

Php: Escape characters

Escape characters are used to print unprintable things like tab, new line and escape special characters like ', ".

 

For example,

\n for a new line

\t for a tab

\' for single quote

\" for a double quote

\\ for a backslash

\$ for a $ sign

 

Example

$msg1 = "It's my book\n";

$msg2 = 'It\'s my book\n';

 

$msg3 = "\nHello Krishna,\nHi PTR";

 

Single quotes do not process \n, you can confirm the same from below application.

 

escape_chars_demo.php

#!/usr/bin/php

<?php
$msg1 = "It's my book\n";
$msg2 = 'It\'s my book\n';

$msg3 = "\nHello Krishna,\nHi PTR";

echo $msg1;
echo $msg2;
echo $msg3;
?>

 

Output

$./escape_chars_demo.php 

It's my book
It's my book\n
Hello Krishna,
Hi PTR

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment