Skip to content
Advertisement

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

Advertisement

Answer

Try somothing like this (add some if else statment):

public function convert_to_xml($rs = NULL /* this is by default */ ) {
     if(isset($rs) && !empty($rs)){
        var_dump($rs);
     } else {
        echo 'Variable not passed or empty';
     }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement