0

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();
5
  • Your class doesn't have a function called check(). Commented Jan 2, 2021 at 13:06
  • Hi I have corrected the question, please check @ADyson Commented Jan 2, 2021 at 13:07
  • @ADyson Array ( [0] => 2 [1] => 3 [2] => 5 ) Notice: Trying to access array offset on value of type null in /home/u483428433/domains/ytsubme.com/public_html/ ...on line 8 Commented Jan 2, 2021 at 13:08
  • Ok thanks. Your one() method doesn't have a return statement. You need return $lol; at the end of it. Commented Jan 2, 2021 at 13:08
  • Thank you somuch @ADyson, ,its working now. :D Commented Jan 2, 2021 at 13:12

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.