Jump to content

Archived

This topic is now archived and is closed to further replies.

nadeemshafi9

write PHP code using fwrite()

Recommended Posts

hi guys

i need to write the entire following code to a file using fwrite()

[code]$data = "".
"<?php".
"$i = 'hello'".
"print $i;".
"?>";

$i=fopen(file)
fwrite($i,$data)[/code]

i tried leading and trailing backslashes and double backslashes they reduce the prob but dont eradicate it. the slashes appear in the file

thanks for any help

Nadeem

Share this post


Link to post
Share on other sites
Use HEREDOC syntax:
[code=php:0]$data = <<<PHP
<?php
\$i = 'hello';
print \$i;
?>
PHP;

$i = fopen(file);
fwrite($i, $data);[/code]

Share this post


Link to post
Share on other sites
hi i tried ur sugestion it didnt work but i read up on the heredoc syntax on php.net and it said in cases like mine i can also use single quotes which work well.

thancks though this heredoc syntax looks interesting and is new to me iim glad u brought it up

thanks

Share this post


Link to post
Share on other sites
HEREDOC is very html for large text/html code blocks. Saves you having to escape quotes etc. But alot of people dont know this syntax exists.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.