为什么我不能写"$"fwrite中的字符


PHP fwrite function why i cannot write "$" character in fwrite

这段代码的问题是什么?为什么我不能使用"$"在fwrite函数中?

fwrite($dosya_index,"$al_".$bol_radiopart[1]." = $_POST['".$bol_radiopart[1]."'];");

这是因为PHP变量被插入到用双引号括起来的字符串中。这意味着以双引号中$符号开头的单词将被作为变量处理,如果存在这样的变量,则在字符串中替换其值。转义$符号('$)或在不应用插值的地方使用单引号。

我不是100%确定你想要实现什么,但我猜你正试图将一些代码写入文件或其他东西。
如果你想用$之类的,你必须用'代替",像这样:

fwrite($dosya_index,'$al_' . $bol_radiopart[1] . ' = $_POST[' . $bol_radiopart[1] . '];');

请看下面的例子:

//$write is a handler for the fopen() method you can use any $var you want and encode info too before execute
      $write=fopen($filepath,"w") or die ('Something went wrong'); // "w" flag is for read and write mode, create file if doesn't exists and delete file content before write
      fwrite($write, $filecontent); //write all data saved in the string "$filecontent" into the file
      fclose($write); // close the file

阅读更多:https://www.php.net/manual/es/function.fwrite.php