我希望这些变量用它们的值填充,但是在config.php文件中它自己编写变量名,我希望$ host转换为' localhost'在config.php文件中使用单引号。
$handle = fopen('../config.php', 'w');
fwrite($handle, '
<?php
$connection = mysql_connect({$host}, {$user}, {$pass});
?>
');
fclose($handle);
答案 0 :(得分:4)
你做不到。单引号do not interpolate variables。这是将它们与双引号区分开来的主要因素。使用双引号(或其他内容,例如sprintf
)。
答案 1 :(得分:2)
如果在单引号中使用变量,它们将表示为字符串而不是变量。
你也可以这样做:
// Get from $_SESSION (if started)
$host = $_SESSION['host'];
$user = $_SESSION['user'];
$pass = $_SESSION['pass'];
$handle = fopen('../config.php', 'w');
// try with the {}
$content = '<?php $connection = mysql_connect('."{$host},"."{$user},"."{$pass});".'?>';
// or you can try this too, but comment out the other one:
$content = '<?php $connection = mysql_connect('."\"$host\","."\"$user\","."\"$pass\");".'?>';
fwrite($handle, $content);
fclose($handle);
答案 2 :(得分:1)
如果你使用双引号,它可以工作:
$handle = fopen('../config.php', 'w');
fwrite($handle, "
<?php
$connection = mysql_connect({$host}, {$user}, {$pass});
?>
");
fclose($handle);