如何使变量在单引号中正常工作? [英] How to make variables work in Single Quotes properly?
本文介绍了如何使变量在单引号中正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望这些变量用它们的值填充,但是在config.php文件中写入变量名本身,我希望像$ host在config.php文件中转换为带有单引号的'localhost'.
I want those variables to be filled with their values, but in config.php file its writing the variable name itself, I want like $host convert to 'localhost' with single quotes in the config.php file.
$handle = fopen('../config.php', 'w');
fwrite($handle, '
<?php
$connection = mysql_connect({$host}, {$user}, {$pass});
?>
');
fclose($handle);
推荐答案
如果在单引号中使用变量,它们将被表示为字符串而不是变量.
If you use variables within single quotes, they will be represented as strings instead of variables.
您也可以这样:
// 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);
这篇关于如何使变量在单引号中正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文