After the function is done, sleep and print text - php

Hello, I have a question about PHP sleep().
This is my source code:
if (function here) {
functions.
echo 'create ok';
}
sleep(10);
if (function here) {
functions.
echo 'Move Finished';
}
when I want to run this functions, the system sleeps for 10 seconds and after that shows:
create ok
Move Finished
But what I want is that when the function is done, show and print (create ok), after that sleep for 10 seconds and after that show and print (Move Finished).
What should I do?

If you're using this code on a web page, you will need to flush the output buffer, i.e.
echo "create ok\n";
flush();
sleep(10);
echo "move finished\n";
See also: flush()

Related

How to delay 2 seconds in PHP to include?

I want this PHP script to wait 2 sec before including an sp.php (server panel).
The sleep(2); doesn´t work for me (It doesn´t do anything at all.)
My code:
<?php
$cookie_name = "sc";
?>
<?php
if(!isset($_COOKIE[$cookie_name])) {
include 'noserver.php';
} else {
echo "Server found";
sleep(2);
include 'sp.php';
}
?>
Any ideas? It`s for the server hosting web.
You can use Javascript with setTimeout and window.location.href to redirect to the file.
Example:
<?php
if (!isset($_COOKIE[$cookie_name])) {
include 'noserver.php';
} else {
echo 'Server found';
echo '<script>setTimeout(function(){window.location.href =
"http://yourwebsite.com/sp.php"}, 2 * 1000);</script>';
}
?>
You probably think it is doing nothing at all because you don't see the output until the include has been completed. Try putting a flush command and flushing any buffering after writing "Server found":
print "Server found\n";
// in the very worst case you might be forced to send "<!-- GARBAGE -->"
// with enough "garbage" to force any intermediate buffers to flush.
if (ob_get_level()) {
ob_flush();
}
flush();
...
However, as it has been suggested, this is likely a XY problem. You want something else to happen, and a server side delay of 2 seconds is probably not the best way to achieve this.
For example, if you want a quick "Server found" message to immediately appear, and then a slower page to load (even if it is the same page), you could do this with a session flag or using jQuery and a DOM load.
In the first case you load the same page twice, the first time showing the message, setting the flag and reloading using Location or a Meta reload (echo "<meta http-equiv="refresh" content="2" />)). The second time, with the flag set, you un-set the flag and run the include(). Problem solved with no server sleep() overhead.

Print Output as it is generated in PHP

Here is the pseudo code of what I am doing:
<?php
for ($x=0;$x<5;$x++)
{
$result=shell_exec("python code.py");
echo $result;
sleep(15);
}
?>
Here is the code.py pseudo code
try:
condition
print "successful"
except:
print "not successful"
What I am trying to do is, each time the python code is run print its output on the web page instantly, then initiate the sleep timer and repeat.
Instead what I am getting now is the web page stalls for a cumulative 15*5 secs, and displays output of the python code that was run 5 times in one go.
Any suggestions on how this can be done

Why is a PHP script waiting "to finish script" before any echo/output?

Considering a simple script
<?php
echo "hi";
foreach ($_GET['arr'] as $a)
{
echo $a ."<br>";
}
echo "<p>Masel tov</p>";
foreach ($_GET['arr2'] as $a)
{
echo $a ."<br>";
}
i expect the script to echo continuously. Instead the script does echo all-at-once when finished. Even the first "hi" gets echoed after 1 minute when the script finishes.
Is there a setting to prevent this from happen or why is that so?
Depending on your config, output is cached until completion. You can force a flush with either ob_flush() or flush(). Sadly many modern browser also dont update until page load is complete, no matter how often you flush.
flush http://php.net/manual/en/function.flush.php
ob_flush http://php.net/manual/en/function.ob-flush.php
Configuration Settings for PHP's output buffering.
http://www.php.net/manual/en/outcontrol.configuration.php
There is a function ob_implicit_flush that can be used to enable/disable automatic flushing after each output call. But have a look at the comments on the PHP manual before using it.
Check with this
if (ob_get_level() == 0)
{
ob_start();
}
for ($i = 1; $i<=10; $i++){
echo "<br> Task {$i} processing ";
ob_flush();
flush();
sleep(1);
}
echo "<br /> All tasks finished";
ob_end_flush();
If you want displaying the items one by one and keep clean code that works with every server setup, you might consider using ajax. I don't like flushing the buffer unless there are no other options to accomplish the task.
If your project isn't a webproject you might consider running your code in the php console (command line) to receive immediate output.
PHP sends, as you noticed, all data at once (the moment the script has finished) - you search for something like this http://de1.php.net/manual/de/function.ob-get-contents.php :
<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>

Sleep() Function not doing what I think it should

First, correct me if i'm wrong but i'm under the assumption that when you run the Sleep() function, it pauses running the script where it is located in the script, not at the beginning. If that is true can someone tell me why the below script waits 5 seconds and then shows both echos at the same time. NOT echo the first statement on page load and then wait 5 seconds and then fire the second echo....
echo "Your account username has been updated, you will now be redirected to the home page!";
sleep(5);
echo "REDIRECT!";
In your code PHP execution will pause for 5 seconds but it will not render itself part by part. i.e. It will not show the first statement and then the second. PHP keeps all its value in output buffer and display them when its finishes execution.
What happens is, it holds the value of first echo in output buffer and then waits for 5 seconds, then is holds another echo output in output buffer and shows all at once.
What you are trying to do is a lot easier in JS.
echo "Your account username has been updated, you will now be redirected to the home page!";
echo "<script> document.setTimeout(function() { document.location('redirect.html'); }, 5000); </script>";

How to refresh display more often for a long script in PHP

I search a way to refresh display more often on a long script.
I.e. :
for($i=0;$i<=100000;$i++) {
<!--
some long process code
-->
echo 'content:'.$i.' - ';
}
I launch it on Chrome. Currently, echo data appeared every 30 seconds. I would like a shorter delay for the refreshing echo display.
Thanks for your time.
To push script's output to the browser call flush() periodically, i.e:
for($i=0;$i<=100000;$i++) {
echo 'content:'.$i.' - ';
if( $i%100 ) {
flush();
}
}
also check your output buffering settings
You could take a look at output buffering.
http://php.net/manual/en/book.outcontrol.php

Categories