1

I'm curious for the reasoning behind the following:

Works with no error:

$contact_data = <<<STRING
<contact>
    <Group_Tag name="Contact Information">
        <field name="First Name">$contact_first_name</field>
        <field name="Last Name">$contact_last_name</field>
        <field name="Email">$contact_email</field>
    </Group_Tag>
</contact>
STRING;

Returns an error:

    $contact_data = <<<STRING
    <contact>
        <Group_Tag name="Contact Information">
            <field name="First Name">$contact_first_name</field>
            <field name="Last Name">$contact_last_name</field>
            <field name="Email">$contact_email</field>
        </Group_Tag>
    </contact>
    STRING;

Error:

Parse error: syntax error, unexpected end of file.

As you can see, the only difference is that the second case is indented (4 spaces), and the first is not. I'm curious why...

rnevius
  • 26,578
  • 10
  • 58
  • 86

2 Answers2

2

Basic HEREDOCs: The terminator HAS to be at the start of the line:

echo <<<EOL
foo
EOL; // ok, terminator in column 0


echo <<<EOL
    foo
    EOL; // bad, there's whitespace before the terminator

There is an exception for this, but it's best to pretend like it doesn't exist, and just ALWAYS put the terminator string at the start of the line.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Your string delimiter has to appear in column 1, as follows:

    $contact_data = <<<STRING
    <contact>
        <Group_Tag name="Contact Information">
            <field name="First Name">$contact_first_name</field>
            <field name="Last Name">$contact_last_name</field>
            <field name="Email">$contact_email</field>
        </Group_Tag>
    </contact>
STRING;
danmullen
  • 2,556
  • 3
  • 20
  • 28