I am trying to to use fwrite and put some html codes in a file through php but I also want to include php brackets while writing into the file. Somehow this doesn't work. Here's the code.I just wanted to ask how can i write php statements to a file using fwrite
.
<?php $File = fopen('myfile.txt' "w"); fwrite($File, '<html> <body> <p id='test'>Test</p> <?php echo 'hey'; ?> </body> </html> ') fclose($File); ?>
尝试这样写:
echo "<?php hey ?>"
First of all your fopen
has syntax error: it should be $File = fopen('myfile.txt', "w");
not $File = fopen('myfile.txt' "w");
. Try this:
$File = fopen('myfile.txt', "w");
fwrite($File, "<html>
<body>
<p id='test'>Test</p>
<?php
echo 'hey';
?>
</body>
</html>
");
fclose($File);
the output in myfile.txt
will be:
<html>
<body>
<p id='test'>Test</p>
<?php
echo 'hey';
?>
</body>
</html>
which is valid.
You may try:
<?php
$File = fopen('myfile.txt',"w");
fwrite($File,
"<html>
<body>
<p id='test'>Test</p>"
.'hey'.
"</body>
</html>
"
);
fclose($File);
?>
You seem to been opening the PHP string near id='test
which is the problem here.
Instead, assign all your HTML to a variable to make things easy.
Surround the PHP string with single quotes '
and use double quotes "
in the HTML.
$html = '<div id="test">
<p class="paragraph">This is foo</p>
<p class="paragraph"> and this is ' . $bar .'</p> // You can concatenate with . following a PHP variable
</div>';
fwrite($file, $html);
fclose($file);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.