Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with $_SERVER['PHP_SELF'] in string 1

Status
Not open for further replies.

hansu

Programmer
Mar 12, 2002
89
To build a page dynamically I use a string like the following:
Code:
<?php
$header_0_en = <<<EOD
         <div id="langcontainer">
         <?php echo setLanguage($_SERVER['PHP_SELF']); ?>
         </div>
EOD;
?>
This produces the error: unexpected T_ENCAPSED_AND_WHITESPACE
So far I didn't find a way to set quotes properly to avoid this error. I can use setLanguage($PHP_SELF), which works but I'm not sure whether that is a good idea. (I'm using PHP 5)

Thanks for any assistance.
 
you are using the heredoc syntax so if you want to include arrays in the heredoc you need to use the complex syntax (curly braces).

however, you cannot include expressions within the heredoc syntax (I believe).

Code:
// so either:
$contents = setLanguage($_SERVER['PHP_SELF']);
$header_0_en = <<<EOD
         <div id="langcontainer">
         $contents;
         </div>
EOD;

//or like this

$header_0_en = "
<div  id=\"langcontainer\">" . setLanguage($_SERVER['PHP_SELF'])."
</div>
";
 
there is another option too (which does more or less allow expressions in heredoc, despite what i said above!).

Code:
$header_0_en = <<<EOD
         <div id="langcontainer">
         $contents;
         </div>
EOD
.setLanguage($_SERVER['PHP_SELF']);

the key thing is to honour the heredoc terminator by not including ANYTHING on the terminating line other than the heredoc identifier and a line break (and maybe a semicolon). then on the next line add a concatenate and the expression.

sorry: i should have included this in my previous post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top