Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding why echo doesnt work within a php class function

I'm trying to debug a class and using print_r and echo to view the variables as the script progresses.

However for some reason I can't seem to get any output from within the function, i've declared the function public but can't get any output using print, echo or print_r.

I'm misunderstanding something fundamental here - can anyone help please.

here's some extract from my code:

this call is from within a public function within the class:

$xml_data = $this->convert_to_xml($rs);

The method looks like this:

public function convert_to_xml($rs) {
    echo "test variable:";
    print_r($rs);
}

The print_r was purely to test the values were being passed, but I don't get any output at all

like image 688
Ray Avatar asked Aug 01 '26 19:08

Ray


1 Answers

If you want to return the value to assign it to $xml_data, you must actually return it instead of echoing it. echo send the value directly to the output stream, while return returns the value from the function, so that it can be assigned to a variable or be used in other expressions:

public function convert_to_xml($rs) {
     return "test variable:";
}
like image 74
GolezTrol Avatar answered Aug 03 '26 08:08

GolezTrol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!