In the following code I want to echo
green
outside of public function
.
Public function lol(){
$green ="green";
}
for example i want to echo $green
in the following code.
public function green(){
echo"this is $green";
}
You pass $green
in as a parameter and echo the function return value:
function green($green) {
return "This is ". $green;
}
echo green('green'); //Results in: This is green
echo green('yellow'); //Results in: This is yellow
try the following code :
function __construct() {
parent::__construct();
$green ="green";
}
public function green(){
echo"this is $green";
}
Place the $green variable in constructor.
$green;
class s {
public function lol() {
$GLOBALS['green'] = "green";
}
}
$instance = new s();
$instance->lol();
echo $green;
Use global variables with $GLOBALS
:
function lol(){
$GLOBALS['green'] ="green";
}
lol();
echo $green;