PHP String Best Tutorial In Hindi 2025

PHP String: PHP में String डेटा का एक प्रकार (Data Type) है, जो characters का sequence होता है। Strings में टेक्स्ट डेटा को स्टोर और manipulate किया जाता है। उदाहरण के लिए, “Hello”, “123”, या “PHP Rocks!” सभी strings हैं।

How To Define Sting In Php?

PHP में strings को दो तरीके से डिफाइन किया जा सकता है:

  1. Single Quotes (‘ ‘)
  2. Double Quotes (” “)

Single Quotes (‘ ‘):

  • Single quotes का उपयोग simple strings को डिफाइन करने के लिए किया जाता है।
  • इसमें variables या special characters को interpret नहीं किया जाता।
<?php
  $text = 'यह एक simple string है।';
  echo $text;
?>
PHP
Php String

Double Quotes (‘ ‘):

  • Double quotes का उपयोग complex strings को डिफाइन करने के लिए किया जाता है।
  • इसमें variables और escape sequences (\n, \t, आदि) को interpret किया जाता है।
<?php
  $name = "Vikas";
  $text = "My Name Is $name";
  echo $text;
?>
PHP
Php String

Multiline Strings (Heredoc और Nowdoc)

Php Heredoc String:

PHP में Heredoc का उपयोग स्ट्रिंग को डिफाइन करने के लिए किया जाता है, खासतौर पर जब स्ट्रिंग मल्टीलाइन होती है। यह सामान्य स्ट्रिंग डिक्लेरेशन की तुलना में कोड को अधिक रीडेबल बनाता है। हिंदी या किसी भी अन्य भाषा के टेक्स्ट को Heredoc के अंदर आसानी से लिखा जा सकता है।

heredoc का सिंटैक्स:

<?php
  $str = <<<START
  first line content 
  another content
  START;
?>
PHP

But close करते समय ये ध्यान रहे कि identifier newline के first column में ही हो , उससे पहले न कोई space , न variable कुछ भी नहीं होना चाहिए नहीं तो PHP Fatal Error Generate करेगी।

<?php
  $name = "Vikas";
  $greeting = <<<GREETING
  नमस्ते $name,
  आप कैसे हैं?
  GREETING;
  
  echo $greeting;
?>
PHP
image 22

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
image 23

Heredoc vs Nowdoc

FeatureHeredocNowdoc
Variable ParsingVariable Can Be ParseVariable Can’t Be Parse
Syntax<<<identifier<<<‘identifier’
UseUser For Dynamic StringsUse For Static Strings

Yoga

error: Content is protected!! You are not allowed to copy.