簡體   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