Php 在eod中插入特定元素

Php 在eod中插入特定元素,php,heredoc,Php,Heredoc,我需要在一个文件中插入一个代码,当有一个安装 结果一定是 if (isset($_SESSION['admin']['id'])) { define('DIR_FS_IMAGES', '{$dir_fs_document_root}images/'); define('DIR_WS_IMAGES', '{$http_catalog}images/'); } 在安装过程中,我尝试了这一点,但如果不似乎工作得很好。 如何处理以获得与上述相同的结果 多谢各位 $file_content

我需要在一个文件中插入一个代码,当有一个安装

结果一定是

if (isset($_SESSION['admin']['id'])) {
  define('DIR_FS_IMAGES', '{$dir_fs_document_root}images/');  
  define('DIR_WS_IMAGES', '{$http_catalog}images/'); 
}
在安装过程中,我尝试了这一点,但如果不似乎工作得很好。 如何处理以获得与上述相同的结果

多谢各位

$file_contents = <<<ENDCFG
<?php
if (isset($_SESSION['admin']['id'])) {
define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
}
ENDCFG;

$file\u contents=您必须在herdoc字符串中转义美元符号字符
$
,或者改用字符串

Nowdocs是单引号字符串,就像heredocs是双引号字符串。nowdoc的指定方式类似于herdoc,但在nowdoc内部不进行解析


<?php
$file_contents = <<<'ENDCFG'
<?php
if (isset($_SESSION['admin']['id'])) {
define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
}
ENDCFG;

file_put_contents('/tmp/out.php', $file_contents);
$ php test.php
$ file out.php
out.php: PHP script text, ASCII text
$ cat out.php
<?php
if (isset($_SESSION['admin']['id'])) {
define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
}