繁体   English   中英

使用

[英]using <?php brackets inside an echo statement

我正在尝试使用fwrite并将一些html代码通过php放入文件中,但我也想在写入文件时包括php括号。 不知何故,这行不通。 这是代码。我只是想问我如何使用fwrite将php语句写入文件。

 <?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 ?>"

首先,您的fopen语法错误:应该是$File = fopen('myfile.txt', "w"); 不是 $File = fopen('myfile.txt' "w"); 尝试这个:

$File = fopen('myfile.txt', "w");
fwrite($File, "<html>
        <body>
        <p id='test'>Test</p>
        <?php
        echo 'hey';
        ?>
        </body>
        </html>
        ");
        fclose($File);

myfile.txt的输出将是:

     <html>
        <body>
        <p id='test'>Test</p>
        <?php
        echo 'hey';
        ?>
        </body>
        </html>

这是有效的。

您可以尝试:

<?php
  $File = fopen('myfile.txt',"w"); 
  fwrite($File,
    "<html>
    <body>
    <p id='test'>Test</p>"
    .'hey'.
    "</body>
    </html>
    "
  );
  fclose($File);
?>

您似乎在打开id='test附近的PHP字符串,这是这里的问题。

而是将所有HTML分配给变量以使事情变得容易。

在PHP字符串中用单引号'并在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM