why php cannot show variable in string template under some circumstances? -


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

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -