Monday, 27 March 2017

PHP here document(heredoc)

Another Way of displaying text that you should be aware of, and that's using PHP "here" documents.
A heredoc is just same text inserted directly in a PHP page, between two instances of the same token.

Token:-
              Token is a word such as END then you can display the text in a here document by using the syntax echo<<<TOKEN
Where TOKEN is the word that begins and ends the here document.
Example:- 
<HTML>
<HEAD>
<TITLE>
Display text from PHP
</TITLE>
</HEAD>
<BODY>
<H1>Display text from PHP</H1>
Here's What PHP has to Say:
<BR>
<BR>
<?php
echo<<<END
This example Uses
"here document" Syntax to display all the
text until the ending token is reached
END;
?>
</BODY>
</HTML>
Browser view:- 


And you can see what this produces in a browser. The text in the here document was display in the browser.
Using the token END is by no means necessary; here's the same here document with the token FINISH:

echo<<<FINISH
This example uses
"here document" syntax to display all
the text until the ending token is reached.
 

2 comments: