Click to See Complete Forum and Search --> : PHP equivalent of perl's print <<EOF


nuthead
05-29-2003, 08:13 AM
is there a PHP equivalent of perl's print <<EOF? rather than having to keep putting

?>
...
...
...
<?php

and can i do variables like:
$variable = qq~
...
...
...
~;

I hope that makes sense, im trying to move my scripts over from perl to PHP as i prefer PHP :)

Nevermore
05-29-2003, 09:17 AM
What does that Perl function do?

DaiWelsh
05-29-2003, 09:59 AM
Yes, the 'heredoc' concept also exists in PHP, e.g

$variable = <<<XYZ
........
........
........
........
........
XYZ;

just make sure closing XYZ is the very first thing on the line (including whitespace like space and tabs).

You can sue any (sensible) identifier, XYZ is just an example.

HTH,

Dai

nuthead
05-29-2003, 11:05 AM
Originally posted by cijori
What does that Perl function do?

It lets variables/print commands span more than one line, it's very useful for HTML.

Nevermore
05-29-2003, 12:29 PM
Thenn you can use echo("/*Code
And some more...
And more...
And more...*/");

Just remember to escape the string you are printing.

DaiWelsh
05-29-2003, 11:47 PM
The second advantage of heredoc is that you can use quotes without escaping them which makes the html much more readable.

Regards,

Dai

Nevermore
05-30-2003, 02:50 AM
I though heredocs needed a PHP extension...

DaiWelsh
05-30-2003, 03:02 AM
heredoc is just the name given to that particular syntactic structure, it is still part of PHP so needs to be interpreted as such, as does the echo() example given. In other words these two are equivalent

<?php
$html = "
<table border=\"1\">
<tr>
<td style=\"x\">
$content
</td>
</tr>
</table>
";
echo($html);
?>

and
<?php
$html = <<< EOH
<table border="1">
<tr>
<td style="x">
$content
</td>
</tr>
</table>
EOH;
echo($html);
?>

But the latter example does not require escaping of the quotes which makes life a lot easier for large blocks of html.

For example, I wrote a simple script that my wife uses on her site, but wanted her to be able to edit the html it uses. She is not a programmer and kept breaking the code when I used the first format as she did not really understand when and how to escape things. Since I changed to heredoc and told her just to edit the lines in the middle with no need to escape any quotes it has been trouble free :)

Regards,

Dai

Nevermore
05-30-2003, 03:05 AM
Thankyou.