Use of backslash in php (Chapter 4) of Getting started with php

Do you want better
IT Services ?

Let our experts help you navigate the digital world with tailor-made IT Solutions!

php.net describes use of backslash perfectly

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

In between if you are interested in php learning check this post on php training in delhi. We provide both online and offline classes on php, drupal, wordpress training, SEO and HTML/CSS.

<?php
echo 'this is a simple string';

//output : You can also have embedded newlines in strings this way as it is okay to do but it wont add any line breaks.
echo 'You can also have embedded newlines in 
strings this way as it is
okay to do but it wont add any line breaks.';

// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';

// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';

// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>

One more example on where to use \ in php. Actually I love posting quotes on my Websites and at the same time I love to read quotes by famous writers. Napoleon Hill Once Said

“what your mind can conceive and believe it can achieve”

What if i want to echo this in php

<?php

//Output : what your mind can conceive and believe it can achieve

echo "what your mind can conceive and believe it can achieve";


//Output : "what your mind can conceive and believe it can achieve"

echo "\"what your mind can conceive and believe it can achieve\"";

?>
Scroll to Top