-2

我正在创建一些代码,这些代码将生成一个带有配置类的新页面。我像下面这样使用了heredoc 。所以我想将 $_POST['hostname'] 变量传递到heredoc。我尝试了很多东西,但没有运气。我该怎么做?

<?php
    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 );

?>
4

2 回答 2

1

删除 heredoc 标识符周围的引号。

$PARD_CONFIGURATION_CLASS = <<<EOT

如果您保留引号,则称为 nowdoc。PHP 文档是这样说的:

Nowdocs 是单引号字符串,就像 heredocs 是双引号字符串。nowdoc 的指定与heredoc 类似,但在nowdoc 内部不进行解析

于 2013-04-11T13:34:06.380 回答
0

你是这个意思吗:

$variable = "Test"; //Can be any variable; may be $_POST['name']
//Using '{' and '}' special characters in PHP strings.
$string = <<<HEREDOC
{$variable}
HEREDOC;

echo $string ;
于 2013-04-11T13:44:55.923 回答