php - 如何用 fopen 编写变量名称?
问题描述
我正在尝试写入配置文件,但无法打印的问题$names
<?php
$myfile = fopen("config.php", "w") or die("Unable to open file!");
$txt = "$dbname = '1'; $dbuser = '2'; $dbpass = '123'; $dbhost = 'localhost';\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
输出显示我
= '1'; = '1'; = '123'; = 'localhost';
我需要他写变量名,需要一些帮助,谢谢!
解决方案
您需要转义 $ 符号:
$myfile = fopen("config.php", "w") or die("Unable to open file!");
$txt = " \$dbname = '1'; \$dbuser = '2'; \$dbpass = '123'; \$dbhost = 'localhost';\n";
fwrite($myfile, $txt); fclose($myfile);
其他明智的使用单引号:
$myfile = fopen("config.php", "w") or die("Unable to open file!");
$txt = ' $dbname = "1"; $dbuser = "2"; $dbpass = "123"; $dbhost = "localhost";\n';
fwrite($myfile, $txt); fclose($myfile);
说明:
php 中的变量 $var 是否在双引号中进行评估,但不是在单引号中。我认为对于表演来说,如果里面的表达式是常量并且不需要评估,那么最好避免使用双引号。
推荐阅读
- maven - 如何将依赖项从本地 gradle 缓存复制到 Windows 计算机上的本地 maven 存储库?
- python - blender 2.92 python,大纲中选择的元素。我不希望它成为
- reactjs - 如何处理 Shopify GraphQL 批量请求并下载响应?
- unity3d - 很好地弯曲轨道和道具的方法?
- html - 显示 flex 不能在 codepen 之外工作
- javascript - D3.js v5 - 在线性比例上创建一个相对的、可缩放的类似时间轴的轴
- github - 如何在许多 GitHub 存储库中跟踪问题和 PR
- nginx - Nginx 502 重定向到其他页面
- android - 如何在另一个文件中使用该变量?(反应原生)
- java - 如何解决 Spring Boot Procfile heroku 错误