String series of  characters या group of characters होती है , PHP में मुख्यता चार  तरह से हम string define करते हैं -

  1. Single Quoted
  2. Double Quoted .
  3. heredoc syntax .
  4. newdoc syntax .

PHP Single Quoted String

Single Quoted  string PHP में String define करने  सबसे आसान तरीका है। जिसके कुछ example आप नीचे देख सकते हैं।

File : string.php

Copy Fullscreen Close Fullscreen
<?php
  $x = "Rahul Rajput";
  echo 'hello';
  echo 'hello $x'; 
  /*It doesn't print the velue of $x */
  echo 'hello {$x}';
?>
Output
hello
hello $x
hello {$x}

PHP Double Quoted String

Double Quoted String के लिए " " use करते हैं इसके अंदर हम PHP variables भी print करा सकते हैं।

See Example

File : string2.php

Copy Fullscreen Close Fullscreen
<?php
  $x = "Rahul Rajput";
  echo "Hello";
  echo "Hello : $x";
?>
Output
Hello
Hello : Rahul Rajput

PHP Heredoc String

PHP में String define करने का ये तीसरा method  है  , heredoc syntax में string को define करने के liye three times less than (  <<<str ) और उसके बाद एक identifier provide किया जाता है , but string हम new line से ही start करते हैं। same identifier का use string quotation end करने जाता है।

$x = <<<START
first line content
another content
START;

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

नीचे कुछ Heredoc syntax define किये गए हैं।

PHP Heredoc String Example

Copy Fullscreen Close Fullscreen
<?php
$x = 10;
/*valid*/
echo <<<START
this is simple example
START;

/* valid (you can assign it to any variable */
$str = <<<START
this is simple example
START;
echo $str;

/* valid (you can print variable inside it just like double quoted string "" */
$str = <<<START
this is simple example $x
START;
echo $str; /*Invalid because before the end of identifier there is some space */
echo <<<START
this is simple example
START;
/*Invalid , because there is a comment in same line */
echo <<<START
this is simple example
START; /*even you can't write comment here*/
/*Invalid , because you have left some space befoer semocolon */
echo <<<START
this is simple example
START ; ?>

तो देखा आपने heredoc syntax कितना strict है , जब तक code पूरी तरह से clean नहीं होगा , fatal error generate होती रहेगी।

PHP Newdoc String

Newdoc String Single quoted string होती है। और यही main difference है heredoc string में और Newdoc string में। heredoc string के अंदर हम varibales print करा सकते हैं जबकि Newdoc string के अंदर variables की value की जगह variable का नाम print हो जाएगा।

Means Newdoc String में parsing नहीं होती है , इसके अलावा एक और difference है यहां हम identifier को single quote में लिखते हैं start करते समय।

echo <<<'START'
write string here
START;

बाकी सभी rule इस पर भी लागू होते हैं जो कि heredoc string के लिए थे , syntax clean होना चाहिए।

See Example

PHP Newdoc String Example

File : newdoc.php

Copy Fullscreen Close Fullscreen
<?php
$x = 10;
/*valid*/
echo <<<'START'
this is simple example <br>
START;

/* valid (you can assign it to any variable just like heredoc string */
$str = <<<'START'
you can assign it to variable <br>
START;
echo $str;

/* valid but variable value will not be print */
$str = <<<'START'
variable value doesn't print here $x
START;
echo $str; ?>
Output
this is simple example
you can assign it to variable
variable value doesn't print here $x

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers