Home   |   About   |   Terms   |   Contact    
Read & Learn
 

What is Here Document in PHP?

PHP Articles

☯ All PHP Articles     ☯ All Articles


A new product.






Popular Google Pages:




This article is regarding
What is Here Document in PHP?
Last updated on: .



◕ What is Here Document in PHP?

We can define strings in our programs in three different ways.
1. Single quoted string
2. Double quoted string
3. Here Document.

◕ What is Here Document?

As mentioned, we can define a string with the here document syntax in PHP.
Let have an example:
echo <<<_html_
<html>
<head><title>PHP</title></head>
<body>
We are in the field of PHP.
</body>
</html>
_html_;

A Here Document begins with <<< and a delimiter word. Here the delimiter word is _html_. It can be any other word as per our choice.
The Here Document ends with the same word i.e. with the same delimiter which was at the beginning of a line. Here it is _html_.


▻ The PHP will print the above example like this:
<html>
<head><title>PHP</title></head>
<body>
We are in the field of PHP.
</body>
</html>

Then the Browser will automatically execute the HTML codes into this example and will finally print:
We are in the field of PHP.


■ Some rules of Here Document:

1. Here Document delimiters can contain letters (a-z, A-Z), numbers (0-9), and the underscore character.

2. The first character of the delimiter must be a letter (a-z, A-Z) or underscore.

3. The delimiter that ends the Here Document must be alone on its line. The delimiter can’t be indented and no whitespace, comments, or other characters are allowed after it.

4. A semicolon is allowed immediately after the delimiter to end a statement. Nothing can be on the same line after the semicolon.

5. Here Document obeys the same escape character and variable substitution rules as Double Quoted Strings.

6. Here Document is useful when we want to define or print a string that contains a lot of text or HTML with some variables mixed in it.

Example:

<?php
$title = 'This page is from RiyaButu.com';
$name = 'Sabitri';
$pen = 5;
$pencil = 10;
echo <<<SKY
<html>
<head><title>$title</title></head>
<body>
$name has total $pen pens and $pencil pencils.
</body>
</html>
SKY;
?>

■ On the browser this will print:

Sabitri has total 5 pens and 10 pencils.



Related articles:
Difference between Single quoted string & Double quoted string
Different way to define string in PHP?
What is Here Document in PHP?
What is PHP?
How Browser communicates with Server?
How PHP Works?
How to use PHP inside HTML?





Popular Google Pages:





Top of the page

Amazon & Flipkart Special Products

   


Top of the page