PHP callback in browser window

Solution:
This is likely because of output buffering.
Output buffering is a mechanism in PHP that holds the output in memory until a later call toflush
the buffer (also happens automatically when the script shuts down).
Output buffering allows you to print content inside of your script, and provide changes to HTTP headers after content has been printed. Normally, once you print content and send it to apache (who, in turn, sends it back to the client), you can no longer modify the HTTP headers. With output buffering, PHP will store this output in memory and won't hand it over to apache until you tell it to.
You should be able to get this to work in one of four ways:
ob_end_clean
at the very top of your PHP script to disable output buffering- Disable
output_buffering
in php.ini - Enable
implicit_flush
in php.ini (not recommended since it makesoutput_buffering
pointless - Call
ob_flush
after yourprint
/echo
statements
Note: There are also other very legitimate uses for output buffering, such as templating - but that's not important here.
With that being said
This won't necessarily work for everyone. Some browsers by default, and others by configuration, won't start loading a page until the connection is closed and all HTML content has been received. If you want this to work across most every browser today - you might be better off making AJAX calls back to your server.
Edit: To tell ifoutput_buffering
is on, a simple call toprint ob_get_level()
will tell you. If that prints a number > 0,output_buffering
is the culprit.
Answer
Solution:
All scripts ran in console come up in real time. This is why it works in the console and not in browser. You have a few options-flush often enough(not a really good solution as most browsers won't be happy about this), use ob_start and on from there or use ajax which is your safest bet I think.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.