2012-04-22 68 views
0

Iam尝试使用以下代码创建具有数组声明的文件。但是变量名称不是由最终创建的页面引起的。

function createFile($fname,$code){ 
    $fp=fopen("products/".$fname,"w") or die("Can not open file"); 
    fwrite($fp,"<?php 
     $pageeInfo[productcode]=".$code."; 
     ?>"); 

} 

回答

4

单引号禁止变量插值。

'$' 

但是不要写自修改代码;改用某种数据库。

0

它看起来像你嵌套php标签。您可以尝试将嵌套的PHP标签取出。

function createFile($fname,$code){ 
    $fp=fopen("products/".$fname,"w") or die("Can not open file"); 
    fwrite($fp, $pageeInfo[productcode].'='.$code); 
+0

感谢您的宝贵时间和建议,但目标是创建一个PHP文件和每个PHP文件中必须包含一个数组。我的意思是我想解决使用fwrite命令创建php文件时的问题。请帮助我 – Sunil 2012-04-22 03:52:12

1

完成这个正确的方法是使用var_export

function createFile($fname, $code) 
{ 
    return file_put_contents(
     "products/".$fname, // filename 
     '<?php $pageInfo = ' // start tag 
     .var_export(array(// we export an array 
      'productcode' => $code, 
      // add more keys if needed here 
     ), true) 
     .';' // end tag is not required 
    ); 
} 
+1

不,正确的做法是使用数据库。但'var_export()'可能工作。 – 2012-04-22 03:57:55

+0

感谢朋友... :) – Sunil 2012-04-22 03:59:39

相关问题