How to make variables work in Single Quotes properly?

一笑奈何 提交于 2019-12-02 07:34:40

If you use variables within single quotes, they will be represented as strings instead of variables.

You can also do it like this:

// 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);

You can't. Single quotes do not interpolate variables. It the major thing that distinguishes them from double quotes. Use double quotes (or something else, such as sprintf) instead.

If you use double quotes it works:

$handle = fopen('../config.php', 'w');
fwrite($handle, "
<?php
$connection = mysql_connect({$host}, {$user}, {$pass});
?>
");
fclose($handle);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!