faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

81 of 92 people (88%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Does Php have anything similiar to Perl's $string = <<END_OF_DATA ...(some text over multiple lines)... END_OF_DATA?

Mar 15th, 2008 21:18
dman, ha mo, Cinley Rodick, Philip Olson, Dave Kingdon, Greg Billock, Okke Tijhuis, Jon Herman, http://www.ttnr.org


PHP supports HERE doc's in PHP 4.  But in PHP you use <<< instead of << 
so for example:

<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

print $str;
?>

Note that it's very important to not have trailing whitespace after the 
<<<EOD or EOD; in our above examples otherwise you'll get a parse 
error!  Also, the following is perfectly legal and differs with HEREDOC 
in that you still have to worry about quotes inside quotes:

$str = "
Example of string
spanning multiple lines
using PHP-string syntax.
";

Because PHP embeds in HTML so nicely often times one will break out of 
PHP mode into HTML mode.  Something like:

<?php
if ($foo) {
?>
<table>
 <tr>
  <td>Hi</td>
  <td>Friend</td>
 </tr>
</table>
<?php
}
?>

More can be read about HERE doc in the manual:

    http://www.php.net/manual/en/language.types.string.php