Tag Archives: php here document

PHP Here document

Here document |pTutorial

Here document use as a special input output redirection to feed a command list to an interactive program.

It is the way to large block of code into a single variable.

Syntax of here document

<?php
$variable_name=<<<EOF
Code here
-------------------
echo "php tutorial";
echo"www.ptutorial.com";
-------------------
-------------------
-------------------
-------------------
-------------------

EOF;
Echo $variable_name;
?>

Following two example explain the concept of here document

Simple example with a lot of echo statement

<?php

Echo” WWW.ptutorial.com”;
Echo “
<ul>”;
	Echo”<LI>PHP tutorial</li>
”;
	Echo”<LI>java tutorial</li>
”;
              Echo”<LI>C example</li>
”;
echo“</uL>”;
echo “
<h3>Created by ptutorial.com</h3>
”;

?>

Output

WWW.ptutorial.com

  • PHP tutorial
  • java tutorial
  • C example

Created by ptutorial.com

Example using here technique

<?php
$here_doc = <<<EOF
<h3> WWW.ptutorial.com</h3>
<ul>
	<LI>PHP tutorial</li>
<LI>java tutorial</li>
<LI>C example</li>
</uL>
<h3>Created by ptutorial.com</h3>
EOF;

echo ($here_doc);
?>

Output

WWW.ptutorial.com

PHP tutorial
java tutorial
C example
Created by ptutorial.com

EOF denote the start and end point of string. This can be any combination of alphanumeric character. All the rule of making variable are applied here.

If you want to learn more about PHP visit : Php tutorial