-2

I'm creating some code that will generate a new page with a configuration class. I used heredoc like below. So I want to pass $_POST['hostname'] variables into the heredoc. I tried a lot of things, but no luck. How can I do it?

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

?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dilini
  • 49
  • 2
  • 7

2 Answers2

1

Remove the quotes around the heredoc identifier.

$PARD_CONFIGURATION_CLASS = <<<EOT

If you keep the quotes it's called a nowdoc. This is how the PHP documentation puts it:

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
szayat
  • 408
  • 3
  • 9
  • this is the not a problem with heredoc nowdoc.i want to to pass the variable – dilini Apr 11 '13 at 13:37
  • Try removing the single quotes around EOT and your code should work. – szayat Apr 11 '13 at 13:56
  • That is not the only problem. The ending `EOT` must be in column 1 (must ***not*** be indented). That makes it uglier, but it is required. Perhaps add this to the answer? [From the documentation](https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc): *"Warning: It is very important to note that the line with the closing identifier* ***must contain no other characters***, *except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. ..."* – Peter Mortensen Apr 27 '20 at 18:55
0

Do you mean this:

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

echo $string ;
sybear
  • 7,837
  • 1
  • 22
  • 38
  • IT's now doc or heredoc whatever.I need to past that $_POST['hostname']; to to the class for later use.I tried but no luck – dilini Apr 11 '13 at 13:47
  • @dilini: See [my comment](https://stackoverflow.com/questions/15949955/how-to-pass-a-post-variable-inside-a-heredoc#comment108731317_15950198) – Peter Mortensen Apr 27 '20 at 18:50