使用php在文件中写双引号?

时间:2011-08-22 14:37:03

标签: php

我想使用fwrite在php中编写以下字符串,但我不能写 要写的字符串是$config['xxx']: 它显示错误 有人可以帮助我吗?

这是我的代码

$stringData = "$config['xxx']";
 fwrite($fh, $stringData);

我有以下错误

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\xampplite\htdocs\PHP test\filecreate.php on line 6

6 个答案:

答案 0 :(得分:3)

试试这个


    $stringData = '"'.$config['c'].'"';
     fwrite($fh, $stringData);

答案 1 :(得分:2)

$stringData = "{$config['xxx']}"; //<<-- wrap with {...}
fwrite($fh, $stringData);

或者只是:

$stringData = $config['xxx'];
fwrite($fh, $stringData);

或者,如果您确实要将$config['xxx']写入文件,那么您必须进行转义:

$stringData = "\$config['xxx']";
fwrite($fh, $stringData);

答案 2 :(得分:2)

问题是正在解释字符串的内容。你需要用这样的斜杠来逃避'$':

$stringData = "\$config['c']";
fwrite($fh, $stringData);

答案 3 :(得分:2)

请记住,php会尝试用双引号解析$___格式。 E.g。

$foo = 'bar';
echo "$foo"; // outputs bar because it parses the variable

为避免这种情况,您可以使用字符串中的$来转义\$。 e.g。

$stringData = "\$config['xxx']";

也就是说,如果您希望文字文本 $config['xxx']显示在文件中。如果您希望文件中的{em>值 $config['xxx'],您可以使用大括号或只为其分配该变量:

// just the variable
$stringData = $config['xxx'];

// with braces so the interpreter can parse it correctly
$stringData = "{$config['xxx']}";

答案 4 :(得分:0)

尝试使用单个qoutes ...双qoutes告诉PHP检查其中是否有任何变量。因此,如果您有字符串,则应始终使用单引号。即使它中有变量...因为,你需要将变量拉出字符串,就像它必须是:$ bla ='我有一些'。$ variable。'这里';

所以在你的情况下,试试:$ stringData ='$ config [\'xxx \']';

答案 5 :(得分:0)

如果你需要输出包含"的$ config ['xxx']中的值,这个语法很有用:

$stringData = "\"{$config['xxx']}\"";