← Back to PHP Course | Chapter 6: Strings | Lesson 2 of 8

PHP Escape Characters

कुछ characters सीधे आपकी उम्मीद के मुताबिक किसी string के अंदर type नहीं किए जा सकते -- double-quoted string के अंदर एक literal double quote string को जल्दी खत्म कर देगा, और आपके keyboard पर "new line" या "tab" के लिए एक literal character जैसी कोई key नहीं है। Escape sequences इसे solve करते हैं: एक backslash के बाद एक specific letter या symbol PHP को backslash को literally interpret करने के बजाय वह special character insert करने को कहता है।
Syntax
php
"text with \"quotes\" inside"
"line one\nline two"    // \n newline
"column\tcolumn"         // \t tab
"path\\to\\file"        // \\ backslash

किसी String के अंदर Quotes Escape करना

किसी double-quoted string के अंदर एक literal double quote शामिल करने के लिए, इसके पहले एक backslash लगाएँ: \"। बिना backslash के, PHP quote को string का अंत मानता है, जो लगभग हमेशा आसपास के code को एक parse error से तोड़ देता है।

उदाहरण: Escaping Quotes Inside a String

php
<?php
// Print "She said \"hello\" to me." to the output
echo "She said \"hello\" to me.";
?>

\n और \t के साथ Newlines और Tabs

\n एक line break insert करता है और \t एक horizontal tab insert करता है, दोनों invisible whitespace characters हैं जिनकी कोई direct keyboard key नहीं है -- ये सिर्फ double-quoted strings (या heredoc syntax) के अंदर काम करते हैं, single-quoted वालों में नहीं।

उदाहरण: Newlines and Tabs with \n and \t

php
<?php
// Print "Line one\nLine two\tTabbed" to the output
echo "Line one\nLine two\tTabbed";
?>

खुद Backslash को Escape करना

क्योंकि backslash खुद escape character है, किसी string में एक literal backslash शामिल करने के लिए इसे भी escape करना ज़रूरी है: \\। यह खासतौर पर Windows file paths, regular expression patterns, और किसी भी ऐसे text के लिए मायने रखता है जिसे खुद backslashes शामिल करने हों।

उदाहरण: Escaping the Backslash Itself

php
<?php
// Print "C:\\Users\\Alice" to the output
echo "C:\\Users\\Alice";
?>

Double-Quoted Strings में Dollar Sign Escape करना

Double-quoted strings automatically variables interpolate करती हैं -- अंदर एक bare $name उस variable की value से replace हो जाता है।

Interpolation trigger किए बिना एक literal dollar sign के बाद कुछ ऐसा शामिल करने के लिए जो variable name जैसा दिखे, इसे \$ की तरह escape करें।

उदाहरण: Escaping the Dollar Sign in Double-Quoted Strings

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "The price is \$name, not $name" to the output
echo "The price is \$name, not $name";
?>

Single बनाम Double Quotes: सही चुनना

क्योंकि single-quoted strings सिर्फ \\ और \' को पहचानती हैं, वे बिना variables या special characters वाले plain text के लिए simpler और मामूली रूप से तेज़ हैं।

Double-quoted strings, जो पूरा escape sequence set और variable interpolation दोनों support करती हैं, तब बेहतर choice हैं जब किसी string को newlines, tabs, या embedded variable values चाहिए हों।

उदाहरण: Single vs Double Quotes: Choosing the Right One

php
<?php
// Print 'Plain text with no variables' to the output
echo 'Plain text with no variables';
// Print "\n" to the output
echo "\n";
// Print "Text with a newline\n" to the output
echo "Text with a newline\n";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. single-quoted strings के अंदर \n और \t जैसे escape sequences इस्तेमाल करना, जहाँ PHP सिर्फ \\ और \' पहचानता है -- बाकी सब कुछ, \n सहित, दो literal characters की तरह treat होता है।
  2. किसी double-quoted string के अंदर एक double quote escape करना भूल जाना, जो string को जल्दी खत्म कर देता है और आमतौर पर एक parse error का कारण बनता है।
  3. single-quoted strings के अंदर जिन characters को इसकी ज़रूरत नहीं उन्हें over-escape करना, जैसे 'It\s लिखना जब एक simple double-quoted "It's" escape को पूरी तरह avoid कर देता।
चैप्टर सारांश
  • Escape sequences एक backslash के बाद एक character इस्तेमाल करते हैं, जैसे \n (newline), \t (tab), \" (literal quote), और \\ (literal backslash)।
  • Double-quoted strings escape sequences का पूरा set support करती हैं; single-quoted strings सिर्फ \\ और \' पहचानती हैं।
  • आपको किन characters की ज़रूरत है इसके आधार पर single बनाम double quotes चुनना अक्सर सब कुछ escape करने से simpler है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.