PHP Escape Characters
In this page:
"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
// Print "She said \"hello\" to me." to the output
echo "She said \"hello\" to me.";
?>
Login to try C/C++/Java/PHP code in the editor
\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
// Print "Line one\nLine two\tTabbed" to the output
echo "Line one\nLine two\tTabbed";
?>
Login to try C/C++/Java/PHP code in the editor
खुद Backslash को Escape करना
क्योंकि backslash खुद escape character है, किसी string में एक literal backslash शामिल करने के लिए इसे भी escape करना ज़रूरी है: \\। यह खासतौर पर Windows file paths, regular expression patterns, और किसी भी ऐसे text के लिए मायने रखता है जिसे खुद backslashes शामिल करने हों।
उदाहरण: Escaping the Backslash Itself
<?php
// Print "C:\\Users\\Alice" to the output
echo "C:\\Users\\Alice";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "The price is \$name, not $name" to the output
echo "The price is \$name, not $name";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
- single-quoted strings के अंदर \n और \t जैसे escape sequences इस्तेमाल करना, जहाँ PHP सिर्फ \\ और \' पहचानता है -- बाकी सब कुछ, \n सहित, दो literal characters की तरह treat होता है।
- किसी double-quoted string के अंदर एक double quote escape करना भूल जाना, जो string को जल्दी खत्म कर देता है और आमतौर पर एक parse error का कारण बनता है।
- 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 है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: