开发者

write double quote in file using php?

I want to write the following string in php using fwrite but i cant write that string to be write is $config['xxx'] : it shows the error can anybody help me?

here is my code

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

I have the following error

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


try this


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


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

Or just:

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

OR, if you actually want to write $config['xxx'] to the file then you have to do escapes:

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


The problem is that the content of your string is being interpreted. You need to escape the '$' with a slash like this:

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


Remember that php will try to parse $___ formats in double quotes. E.g.

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

To avoid that, you can escape the $ using \$ within the string. e.g.

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

That is to say, if you want the literal text $config['xxx'] to show up in the file. If you want the value of $config['xxx'] in the file, you can use braces or just assign it that variable:

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

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


Write Double quotation inside single Single quotation

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


try using the single qoutes...The double qoutes tell PHP to check if there are any variables in it. Therefore, you should always use the single quotes if you have a string. Even if there are variables in it...because, than you need to pull the variable out of the string, like it must be: $bla = 'I have some '.$variable.' here';.

So in your case, try: $stringData = '$config[\'xxx\']';


If you need to output the value in $config['xxx'] wrapped with "'s, this syntax is usefull:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜