Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print or echo to filehandle to write file

Status
Not open for further replies.

leehinkleman

Programmer
Feb 14, 2002
83
NZ
Could someone please tell me the PHP equivalent of the Perl

open(ABC,">abc.txt");
print ABC <<&quot;EOF&quot;;

# one blank line(above), and then lines of html...
EOF
exit;
close(ABC);


This PHP code:

echo <<<EOF
# lines of html
EOF;

displays 'lines of html' OK, but can that echo, or print, output the 'lines of html' to a file?

Thanks for your help.
 
PHP handles file I/O slightly differently.
Code:
[tt]
    $fp = fopen('abc.txt','w');
    $content = &quot;\n&quot;;
    $content .= &quot;# lines of html&quot;;
    $content .= ...etcetera

    $length_written = fwrite($fp,$content);
    fclose($fp);

    echo $length_written . &quot; lines were written<br>&quot;;
    echo &quot;Content Written: &quot; . $content;
[/tt]

OR

Code:
[tt]
    $fp = fopen('abc.txt','w');

    fwrite($fp,&quot;\n&quot;);
    fwrite($fp,&quot;# lines of html&quot;);
    fwrite($fp,&quot;...etcetera&quot;);

    fclose($fp);
[/tt]

The first method is best because even after writing content to the file, you can reuse that data elsewhere.

Chad. ICQ: 54380631
online.dll
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top