簡體   English   中英

用PHP編寫一個PHP文件

[英]Write a PHP file in a PHP

我正在嘗試從php創建一個新的php文件。

我的最終php文件必須如下所示:

<?
    $text_1 = "Text";
    $text_2 = "Banana";
?>

實際上我的php生成它如下:

$file = fopen("../lang/_____fr-CA.php", "w") or die("Unable to open file!");

<?
    $datas .= '$text_1 = "Text"; \n ';
    $datas .= '$text_2 = "Banana"; \n ';

    $datas = nl2br("&lt;? \n".$datas."\n ?&gt;");

    fwrite($file, $datas);
    fclose($file);
?>

我的問題是生成的php看起來像這樣:

&lt;? <br />
$text_1 = "Text"; \n$text_2 = "Banana"; \n<br />
 ?&gt;

您需要將\\n用雙引號引起來,它不會被視為單引號中的換行符。

出於兼容性目的,您永遠不要使用簡短的開放標記<? ,您只能使用<?php

您可以使用類似Heredoc這樣的更“優雅”的方式來編寫文件:

<?
  $datas <<<eof
<?php
  $text_1 = "Text";
  $text_2 = "Banana";
?>
eof;

  fwrite($file, $datas);
  fclose($file);
?>

您需要寫入所需的數據。 就您而言,您正在編寫HTML。 您應該寫出應有的確切PHP代碼。 您不需要nl2br 您還需要了解, 換行符必須用雙引號引起來 最后, ?>是可選的,我建議您為純PHP文件忽略它。

$datas = "<?php \n" .
    '$text_1 = "Text"; ' . "\n" .
    '$text_2 = "Banana";';
fwrite($file, $datas);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM