html - php variable not working in a heredoc -
i wrote heredoc 3 weeks ago working great, , until today used 5 php variables.
here 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 6 (6) php variables in above heredoc. first 1 ($sitepath) not work. added today.
for reason, server not replacing '$sitepath' (the first php variable in heredoc) correctly because when browser receives above page, error generated:
notice: undefined variable: sitepath in c:\xampp\htdocs\arti-facks\showitem.php on line 221
here 5 php variables have been in heredoc , working fine 3 weeks -- being correctly replaced values before html sent browser:
- $thisfilename
- $theloggedinuser
- $itemname
- $thelinkid
- $itemera
just how know above php variables being correctly resolved on server, associated values being put heredoc sent browser?
simple -- 'view page source' , instead of seeing '$thisfilename', or '$itemname', etc. in html code sent server -- instead see associated values in above heredoc html.
for example, page source shows me '$thisfilename' in heredoc got correctly resolved "showitem.php."
my '$sitepath' declared in global include file called "globals.php" , in file, $sitepath declared this:
$sitepath = "http://localhost/arti-facks";
and @ top of showitem.php have had, past 3 weeks, include statement pull in globals.php:
require_once 'globals.php'; // variables , statics used throughout
so today added $sitepath declaration (above) inside of globals.php
to prove showitem.php able 'see' newly-declared $sitepath added globals.php, echo variable -- sure enough, showitem.php 100% able 'see' newly-declared $sitepath.
and yet -- despite -- , despite 3 weeks of using above heredoc 5 other php variables -- heredoc not getting $sitepath.
when 'view page source' here's see $sitepath line of heredoc above:
<img src="/images/logo-landing2.png" />
you can see $sitepath part before /images/logo-landing2.png blank or missing.
why have i, 3 solid problem-free weeks, been getting 5 successfully-resolved php variables in above heredoc, yet add 6th php variable today , it's if
"goodness, quota php variables in heredoc there maxed out @ 5. , see you're trying use 6th php variable -- thinking."
since heredoc called inside function, global variable $sitepath
not in scope. @ top of function, use global
keyword:
function yourfunction() { global $sitepath; // build heredoc }
or use $globals
array inside heredoc:
echo <<<itemdetails_heredoc <img src="{$globals['sitepath']}/images/logo-landing2.png" /> <form action="$thisfilename" method="post" name="arti-form" id="arti-formid"> ...snip... itemdetails_heredoc;
most prefereable: function parameter
or preferable above either option, pass $sitepath
variable functions need parameter.
function yourfunction($sitepath) { // $sitepath in scope without // reaching global namespace }
Comments
Post a Comment