3

I am using heredoc to create a php file which needs to use heredoc in the generated file as the file will create other files.

The <<<EOD bit works fine and I can see all the variables properly escaped.

The problem I'm having is the nested heredoc <<<EOL where \$languageStrings isn't escaped

If I use \$languageStrings and run the generated file ddd.php, the output is this:

<?php
 = Array (
    'LBL_LOCATION_INFORMATION' => 'Basic Information',
    'LBL_CUSTOM_INFORMATION' => 'Custom Information',
    'SINGLE_' => ''
);

if i use \\$languageStrings trying to esacpe the backslash i get:

<?php
\ = Array (
    'LBL_LOCATION_INFORMATION' => 'Basic Information',
    'LBL_CUSTOM_INFORMATION' => 'Custom Information',
    'SINGLE_' => ''
);

How can I correct this so i get $languageStrings = Array (? I did think about trying to open the file and insert into a specific line but the file will always be different.

I'm open to suggestions

Here is an example of my heredoc

$text = <<<EOD
This is some text
This is some more text

This is a \$variable //outputs this is a $variable

This is some more text
EOD;

$text .= <<<EOD
\n
\$targetpath = 'modules/' . \$moduleInstance->name;

if (!is_file(\$targetpath)) {
    mkdir(\$targetpath);
    mkdir(\$targetpath . '/language');
    // create the language file
    \$languagepath = 'languages/en_us';

    \$languageContents = <<<EOL
<?php
\$languageStrings = Array (
    'LBL_LOCATION_INFORMATION' => 'Basic Information',
    'LBL_CUSTOM_INFORMATION' => 'Custom Information',
    'SINGLE_\$moduleInstance->name' => '\$moduleInstance->name'
);
EOL;

    file_put_contents(\$languagepath . '/' . \$module->name . '.php', \$languageContents);
}
EOD;

file_put_contents('ddd.php', $text);

2 Answers 2

3

I fixed it by creating a variable outside of the heredoc

$strings = '\$languageStrings';

and replacing \$languageStrings with $strings = Array ( in the heredoc string

The output is what I expected

Sign up to request clarification or add additional context in comments.

Comments

2

Need a single quotes like Heredoc. This name is Nowdoc. Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.

$foo = "bar";
echo <<<STR
str $foo
STR;

echo <<<'STR'
str $foo
STR;

OR

echo "str $foo";
echo 'str $foo';

Expected Result: ->str bar ->str $foo

Since PHP 5.3.0, the opening Heredoc identifier may optionally be double quotes

<<<"FOO"
FOO;

same as

<<<FOO
FOO;

I prefer use every time double-quoted heredoc for intelligibility, although it is optional.

Doc says: php.net

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. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML construct, in that it declares a block of text which is not for parsing.

A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.