开发者

Variable undefined when creating heredoc string

I am using the EOF function开发者_如何学运维 in my php code.

But I have a problem.

When I try to call a variable from another file, it simply ignores it.

I have a variable in another file(header.inc.php) that looks like this:

$site['url']               = "http://www.mydomainname.com";

Then I put the require function where the EOF code is:

require_once('../../inc/header.inc.php');

The file where the EOF code is , looks like this:

function getServices() {
$sCode .= <<<EOF
<a href="{$site['url']}" class="amenu">Home</a>
EOF;
return $sCode;
}

The variable $site['url'] is empty when I then call that function..

This is driving me crazy! Is there any reason why the EOF code should ignore that variable??


Try global $site; as the first line in your function. Any global variables are not automatically visible in PHP unless you bring them into the function as globals.


This is not related to heredoc syntax. It's a simple variable visibility issue.

getServices() cannot "see" $site['url'] (or $site) in general.

  • If $site is a global (ew), write global $site; as the first line of your function, in order to bring it into scope.

  • Otherwise, pass it as a function parameter to getServices().

BTW, $sCode .= ... should probably be $sCode = ..., if I'm right in guessing that you're making a new string variable, not appending to an existing one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜