I wrote a heredoc three weeks ago that was working great, and until today it used five PHP variables.
Here is the heredoc:
echo <<<itemDetails_HEREDOC
<img src="$sitePath/images/logo-landing2.png" />
<form action="$thisFilename" method="post" name="arti-form" id="arti-formId">
<label style="display: inline-block; width: 140px">OWNER NAME:</label><input type="text" style="width: 300px"
id="ownerId" name="ownerName" readonly="readonly" value="$theLoggedInUser" /><br />
<label style="display: inline-block; width: 140px">THE ITEM:</label><input type="text" style="width: 300px"
readonly="readonly" id="itemNameId" name="itemName" value="$itemName" /><br />
<label style="display: inline-block; width: 140px">LINK:</label><input type="text" style="width: 300px"
readonly="readonly" id="itemLinkId" name="link" value="$theLinkID" /><br />
<label style="display: inline-block; width: 140px">ERA:</label><input type="text" style="width: 70px"
readonly="readonly" id="itemEraId" name="itemEra" value="$itemEra" /><br />
itemDetails_HEREDOC;
There are six (6) PHP variables in the above heredoc. The first one ($sitePath) does not work. I just added it today.
For some reason, the server is not replacing ‘$sitePath’ (the first PHP variable in my heredoc) correctly because when the browser receives the above page, an error is generated:
Notice: Undefined variable: sitePath in C:\xampp\htdocs\Arti-facks\showItem.php on line 221
Here are the five PHP variables that have been in the heredoc and working fine for three weeks — they are being correctly replaced with their values before the HTML is sent to my browser:
- $thisFilename
- $theLoggedInUser
- $itemName
- $theLinkID
- $itemEra
Just how do I know the above PHP variables are being correctly resolved on the server, with their associated values being put into the heredoc then sent to the browser?
Simple — I do a ‘view page source’ and instead of seeing ‘$thisFilename’, or ‘$itemName’, etc. in the HTML code sent by the server — I instead see their associated values in the above heredoc HTML content.
For example, the page source shows me that ‘$thisFilename‘ in the heredoc got correctly resolved to “showItem.php.”
My ‘$sitePath’ is declared in a global include file called “globals.php” and in that file, $sitePath is declared like this:
$sitePath = "http://localhost/Arti-facks";
And at the top of showItem.php I have had, for the past three weeks, this include statement to pull in globals.php:
require_once 'globals.php'; // Variables and statics used throughout
So today I added the $sitePath declaration (above) inside of globals.php
To prove that showItem.php is able to ‘see’ the newly-declared $sitePath I added to globals.php, I echo the variable — sure enough, showItem.php is 100% able to ‘see’ my newly-declared $sitePath.
And yet — despite that — and despite three weeks of using the above heredoc with its five other PHP variables —
the heredoc is not getting $sitePath.
When I ‘view page source’ here’s what I see for the $sitePath line of the heredoc above:
<img src="/images/logo-landing2.png" />
You can see that the $sitePath part before the /images/logo-landing2.png is blank or missing.
Why have I, for three solid problem-free weeks, been getting five successfully-resolved PHP variables in the above heredoc, yet I add a 6th PHP variable today and it’s as if
“Goodness, your quota for PHP variables in your heredoc there was maxed out at 5. And now we see you’re trying to use a 6th PHP variable — what were you thinking.”
Since the HEREDOC is called inside a function, the global variable
$sitePathis not in scope. At the top of the function, use theglobalkeyword:Or use the
$GLOBALSarray inside the HEREDOC:Most prefereable: a function parameter
Or preferable above either option, pass the
$sitePathvariable into functions that need it as a parameter.