String specification in PHP can be done by using the heredoc syntax. It is different from the single quotation marks strings or double quotation marks strings. The heredoc syntax is a helpful feature in PHP to define string variables containing a lot of words to create text for a web page.
Sometimes you may want to add HTML form tags through PHP strings. In such a case heredoc syntax will come to help you. The defined string will interpolate the variables and HTML tags included in the strings initialized with heredoc syntax. The only thing to care about is the syntax as follows
$strVar=<<<LABEL
{Include the text in various lines}
LABEL;
LABEL is the label that tells the interpreter about beginning of a multiline string. The same label has to be given at the end of the text to mark its termination. The label is user defined and start and end labels must be same. It can be anything you can think of. The ending label in a heredoc statement should not be indented.
Correct way of using label in heredoc syntax
<?php $name ='CSVeda.com'; $str = <<<TTT This is an example using<br> heredoc 'syntax' to understand the convenience of writing a huge amount of text without<br> worrying about the spilling the text on next line. <br> and it actually works. It accepts HTML tags <br> <B>Thanks $name</B> TTT;// Indentation of the ending label is not allowed echo $str; ?>
Output is
This is an example using
heredoc ‘syntax’ to understand the convenience of writing a huge amount of text without
worrying about the spilling the text on next line.
and it actually works. It accepts HTML tags
Thanks CSVeda.com
Incorrect way of using label in heredoc syntax
<?php $name ='CSVeda.com'; $str = <<<TTT This is an example using<br> heredoc 'syntax' to understand the convenience of writing a huge amount of text without<br> worrying about the spilling the text on next line. <br> and it actually works. It accepts HTML tags <br> <B>Thanks $name</B> TTT;// This indentation of end label is wrong echo $str; ?>
Output is
Parse error: syntax error, unexpected end of file in C:\wamp\test\heredoc.php on line 13
Example-heredoc to create a Web Form
<?php $myForm=<<<TM <form action="welcome.php" method="post"> User Name <input type="text" name="usrnm"/><br><br> Password <input type="password" name="pwd"/><br><br><br><br> <input type="submit" value="Login"/> </form> TM; echo $myForm; ?>