why php cannot show variable in string template under some circumstances? -
this question has answer here:
thanks time in advance. newbie in php , encountered strange problem, @ least me strange. showing variable in string template. please see code below:
public function welcome() { $data="everyone"; $b = $this->returntemplate(); $a = "<div>dear $data</div>"; } public function returntemplate() { return "<div>dear $data</div>"; }
i thought both $a , $b should same value <div>dear everyone</div>
in fact $a while $b <div>dear </div>
. puzzled me , wonder why? please explain me?
thanks in advance , feedback welcome!
you encountering 'variable scope'. have defined variable $data in welcome() function, not available anywhere outside of function. overcome this, either move out of function or pass parameter returntemplate function.
more info: http://php.net/manual/en/language.variables.scope.php
Comments
Post a Comment