我正在创建一些代码将生成一个配置类的新页面。我使用heredoc像bellow.So我想将$ _POST ['hostname'] varibles传递给heredoc.I尝试了很多但没有运气。可以帮我?
if(isset($_POST['hostname'])){
$HOSTNAME = $_POST['hostname'];
}
if(isset($_POST['database'])){
$DATABASE = $_POST['database'];
}
if(isset($_POST['dbuser'])){
$USER = $_POST['dbuser'];
}
if(isset($_POST['dbuserpassword'])){
$PASSWORD = $_POST['dbuserpassword'];
}
$pardConfig = new PDO('mysql:host='.$HOSTNAME.';'.'dbname='.$DATABASE, $USER, $PASSWORD, array(
PDO::ATTR_PERSISTENT => true
));
if(isset($pardConfig)){
echo "connected";
}
$SQL =<<<'EOD'
CREATE TABLE IF NOT EXISTS pard_host (
host varchar(255) NOT NULL,
db varchar(255),
db_user varchar(255) NOT NULL,
db_pass varchar(255)
)
EOD;
$sq = $pardConfig->query($SQL);
if ($sq) {
echo 'created';
}
$stmt = $pardConfig->prepare("INSERT INTO pard_host (host, db, db_user, db_pass) VALUES (?, ?, ?, ?)");
$stmt->bindParam(1, $HOSTNAME);
$stmt->bindParam(2, $DATABASE);
$stmt->bindParam(3, $USER);
$stmt->bindParam(4, $PASSWORD);
$stmt->execute();
$PARD_CONFIGURATION_CLASS = <<<'EOT'
class pardConfig{
public $HOSTNAME = echo $DATABASE;;
public $DATABASE = echo $DATABASE;
public $USER = echo $USER;
public $PASSWORD = echo $PASSWORD;
}
EOT;
$PARD_FILE_OPEN = fopen("configuration.php", "w+");
fwrite($PARD_FILE_OPEN,$PARD_CONFIGURATION_CLASS );
?>
答案 0 :(得分:1)
删除heredoc标识符周围的引号。
$PARD_CONFIGURATION_CLASS = <<<EOT
如果保留引号,则称为nowdoc。这就是php文档的用法:
Nowdocs是单引号字符串,heredocs是双引号字符串。类似于heredoc指定了nowdoc,但是在nowdoc
中没有进行解析
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
答案 1 :(得分:0)
你的意思是:
$variable = "Test"; //Can be any variable; may be $_POST['name']
//Using '{' and '}' special characters in PHP strings.
$string = <<<HEREDOC
{$variable}
HEREDOC;
echo $string ;