1

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

2
  • EOF is not a function. You're referring to "heredoc syntax" for delimiting strings. The delimiter you choose (e.g. EOF) is relatively arbitrary. Commented May 23, 2011 at 17:01
  • Also, I would say that it's probably a good idea NOT to use "EOF" as that implies "End of File". Perhaps use EOT (End of Text), as EOF can confuse many programmers who might be reading your code later. Commented May 23, 2011 at 17:10

2 Answers 2

4

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.

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

1 Comment

@DiegoP. if Michael's suggestion solved your problem, consider marking it as the accepted answer.
0

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.

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.