我想让这些变量填满它们的值,但在config.php文件中它写变量名本身,我想像$host转换成'localhost'在config.php文件中用单引号。
$handle = fopen('../config.php', 'w');
fwrite($handle, '
<?php
$connection = mysql_connect({$host}, {$user}, {$pass});
?>
');
fclose($handle);
你不能。单引号不能插入变量。这是它们与双引号的主要区别。请使用双引号(或其他引号,如sprintf
)代替。
如果在单引号内使用变量,它们将被表示为字符串而不是变量。
你也可以这样做:
// 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);
如果你使用双引号,它可以工作:
$handle = fopen('../config.php', 'w');
fwrite($handle, "
<?php
$connection = mysql_connect({$host}, {$user}, {$pass});
?>
");
fclose($handle);