如何使用php将php代码写入文件 [英] how to write php code to a file with php

查看:181
本文介绍了如何使用php将php代码写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的网站的一部分,我需要能够使用php将php代码写入文件. 例如:

For part of my website I need to be able to write php code to a file with php. For example:

$filename = "RtestR.php";
$ourFileName =$filename;
$ourFileHandle = fopen($ourFileName, 'w');



$written =  "
    <html>
    <body>
    <?php
    echo \"I like the color \".$_SESSION['color'].\"!!!!\";
    </body>
    </html>

";

fwrite($ourFileHandle,$written);

fclose($ourFileHandle);

但是,它没有创建文件,而是引发了此错误:

But, instead of creating the file, it throws this error:

解析错误:语法错误,意外的T_ENCAPSED_AND_WHITESPACE,预计在第14行出现T_STRING或T_VARIABLE或T_NUM_STRING

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 14

我做错了什么,将php代码写入文件的正确方法是什么?

我认为我可能需要使自己更加清晰...我希望在加载新创建的文件时确定SESSION.从技术上讲,我不想在此页面上获取会话,而是在我正在创建的页面上!我想将代码写入文件,而不是代码的输出!

What am I doing wrong and what is the right way to write php code to a file?

I think I might need to make myself clearer... I want the SESSION to be determined when the newly created file is loaded. Technically I don't want to get the session on this page, but instead on the page I am creating!!! I want to write the code to the file, not the output of the code!

推荐答案

终于明白了! 我需要转义$符号! 像这样:

Finally figured this out! I needed to escape my $ symbols! Like this:

$written =  "
<html>
<body>
<?php
echo \"I like the color \".\$_SESSION['color'].\"!!!!\";
</body>
</html>

";

不敢相信我没想到;)
谢谢大家!

Can't believe i didn't think of that ;)
Thank you all!

这篇关于如何使用php将php代码写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆