Jump to content


Photo

write PHP code using fwrite()


  • Please log in to reply
3 replies to this topic

#1 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 08:26 PM

hi guys

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

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

$i=fopen(file)
fwrite($i,$data)

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
DONT LAY AN EGG OVER IT

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 August 2006 - 09:32 PM

Use HEREDOC syntax:
$data = <<<PHP
<?php
\$i = 'hello';
print \$i;
?>
PHP;

$i = fopen(file);
fwrite($i, $data);


#3 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 10:33 PM

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
DONT LAY AN EGG OVER IT

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 August 2006 - 10:51 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users