我在字符串连接行中收到此错误Parse错误:语法错误,意外T_CONSTANT_ENCAPSED_STRING in

时间:2012-05-05 23:13:47

标签: php string syntax-error

我想让安装文件使用下面的字符串创建一个配置文件,但我不断收到此错误:解析错误:语法错误,意外的T_CONSTANT_ENCAPSED_STRING ...

$tempcon = '
<?php
// Give Usage string for Config File
if(isset($_GET["gimi"])) {
  die('"' . str_replace('\\','\\\\',__FILE__) . '"'); // <--- The error is on this line!
}
$dbhost = "'. $_POST["dbhost"] .'";             // Database Host Address
$dbname = "'. $_POST["dbname"] .'";             // Database Name
$dbuser = "'. $_POST["dbuser"] .'";             // Database Username
$dbpass = "'. $_POST["dbpass"] .'";             // Database Password

?>';

我做错了什么?

提前致谢,

罗伯特

3 个答案:

答案 0 :(得分:2)

您需要转义字符串中的'

答案 1 :(得分:0)

您的最后四个字符串分配似乎已被破坏。我建议使用heredoc / nowdoc来避免问题。

编辑:除此之外,似乎你正在用php编写一个php文件,动态连接到另一个由用户控制的数据库。这有一种难闻的气味......你能描述一下你想做多少吗?必须有更好的方法来实现它。

答案 2 :(得分:0)

你(当前)获得的解析错误是因为数组索引上的引号 - 尽管你的die()也会给你带来麻烦,正如Musa所说。更好的解决方案:使用heredoc

<?php
$tempcon = <<<PHPCODE
<?php
// Give Usage string for Config File
if(isset({$_GET["gimi"]})) {
    die('"' . str_replace('\\','\\\\',__FILE__) . '"'); // <--- The error is on this line!
}
$dbhost = "'. {$_POST["dbhost"]} .'";             // Database Host Address
$dbname = "'. {$_POST["dbname"]} .'";             // Database Name
$dbuser = "'. {$_POST["dbuser"]} .'";             // Database Username
$dbpass = "'. {$_POST["dbpass"]} .'";             // Database Password

?>
PHPCODE;