isedeasy Posted May 30, 2011 Share Posted May 30, 2011 I have a class that I created, halfway down my page I call a function from the class which prints some code and also changes a variable inside the class. It's a search class basically it prints out the search results and saves the total amount of results into the variable. I want to display the amount of results above the results like so:- echo $Search->results; $Search->GetResults('foo'); The problem is that $Search->results will be equal to NULL until the variable is updated in the function GetResults. Hope that makes sense, am I going about this wrong? Quote Link to comment https://forums.phpfreaks.com/topic/237878-print-class-variable-high-up-the-page/ Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 why not just print $this->results inside your getResults function before it prints the other stuff? Alternatively, you could have your getResults function return a string with the output instead of printing it. This is probably the better option. Then you can do $str = $Search->GetResults('foo'); $res = $Search->results; and use these variables whenever you need, instead of being forced to use the output when you call them. Quote Link to comment https://forums.phpfreaks.com/topic/237878-print-class-variable-high-up-the-page/#findComment-1222405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.