Reputation: 105
How can i return a variable value from one function and pass it to another function in a class and access it ?, im new to php class.
here is the code i have tried
<?php
class BioTool{
public function one(){
$lol =[2,3,5]; print_r($lol);
}
public function two(){
$newarr=$this->one(); //this only return the array but i can't access it, check below.
print_r($newarr[0]); //not working
}
}
$biotool=new BioTool();
$biotool->two();
Upvotes: 0
Views: 56
Reputation: 105
Thanks to @ADyson, i had to return from the function instead of echo.
<?php
class BioTool{
public function one(){
print_r($lol);
return $lol =[2,3,5];
}
public function two(){
$newarr=$this->one();
print_r($newarr[0]); // working
}
}
$biotool=new BioTool();
$biotool->two();
Upvotes: 1