PHP String: PHP में String डेटा का एक प्रकार (Data Type) है, जो characters का sequence होता है। Strings में टेक्स्ट डेटा को स्टोर और manipulate किया जाता है। उदाहरण के लिए, “Hello”, “123”, या “PHP Rocks!” सभी strings हैं।
How To Define Sting In Php?
PHP में strings को दो तरीके से डिफाइन किया जा सकता है:
- Single Quotes (‘ ‘)
- Double Quotes (” “)
Single Quotes (‘ ‘):
- Single quotes का उपयोग simple strings को डिफाइन करने के लिए किया जाता है।
- इसमें variables या special characters को interpret नहीं किया जाता।
<?php
$text = 'यह एक simple string है।';
echo $text;
?>PHP
Double Quotes (‘ ‘):
- Double quotes का उपयोग complex strings को डिफाइन करने के लिए किया जाता है।
- इसमें variables और escape sequences (
\n,\t, आदि) को interpret किया जाता है।
<?php
$name = "Vikas";
$text = "My Name Is $name";
echo $text;
?>PHP
Multiline Strings (Heredoc और Nowdoc)
Php Heredoc String:
PHP में Heredoc का उपयोग स्ट्रिंग को डिफाइन करने के लिए किया जाता है, खासतौर पर जब स्ट्रिंग मल्टीलाइन होती है। यह सामान्य स्ट्रिंग डिक्लेरेशन की तुलना में कोड को अधिक रीडेबल बनाता है। हिंदी या किसी भी अन्य भाषा के टेक्स्ट को Heredoc के अंदर आसानी से लिखा जा सकता है।
heredoc का सिंटैक्स:
<?php
$str = <<<START
first line content
another content
START;
?>PHPBut close करते समय ये ध्यान रहे कि identifier newline के first column में ही हो , उससे पहले न कोई space , न variable कुछ भी नहीं होना चाहिए नहीं तो PHP Fatal Error Generate करेगी।
<?php
$name = "Vikas";
$greeting = <<<GREETING
नमस्ते $name,
आप कैसे हैं?
GREETING;
echo $greeting;
?>PHP
Php Nowdoc String:
PHP में Nowdoc का उपयोग भी Heredoc की तरह मल्टीलाइन स्ट्रिंग को डिफाइन करने के लिए किया जाता है। हालांकि, Nowdoc और Heredoc के बीच मुख्य अंतर यह है कि Nowdoc में वेरिएबल्स को पार्स नहीं किया जाता। यह Static टेक्स्ट को डिफाइन करने के लिए उपयोगी है।
Nowdoc का सिंटैक्स:
<?php
$variableName = <<<'identifier'
आपका टेक्स्ट यहाँ लिखें।
यह मल्टीलाइन स्ट्रिंग हो सकती है।
identifier;
?>PHPध्यान दें कि identifier को सिंगल कोट्स में लिखा जाता है।
Nowdoc में वेरिएबल्स को टेक्स्ट के रूप में Display किया जाता है, न कि उनकी वैल्यू Display को किया जाता है। उदाहरण:
<?php
$name = "Vikas";
$greeting = <<<'GREETING'
नमस्ते $name,
आप कैसे हैं?
GREETING;
echo $greeting;
?>PHP
Heredoc vs Nowdoc
| Feature | Heredoc | Nowdoc |
|---|---|---|
| Variable Parsing | Variable Can Be Parse | Variable Can’t Be Parse |
| Syntax | <<<identifier | <<<‘identifier’ |
| Use | User For Dynamic Strings | Use For Static Strings |

Welcome to HindiTutorials, your go-to platform for learning and mastering concepts in Hindi! Our mission is to make education accessible and simple for everyone by providing high-quality tutorials on programming, technology, and various other topics.