niyamxsept
niyamxsept

Reputation: 105

How to access function value in another function in PHP Classes

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

Answers (1)

niyamxsept
niyamxsept

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

Related Questions