i want introduce some sleep functionality into my code so that i can call a function at a specified(time or interval).
my desired output:
(1) at first 5sec
im called
(2) at 10sec (i,e 5sec + 5 sec)
im called // at first iteration of loop
im called // at second iteration of loop
right now i'm using
function curl_grab(){
echo "im called<br/>";
}
$arr = ['http://ab.com/','http://bc.com/'];
foreach($arr as $el){
curl_grab($el);
sleep(5);
}
but the problem with the above code is it is dumping everything at a time
like so
im called
im called
i want it one by one
here is demo: http://phpio.net/s/ggr
If you are using a browser to test this code, you will be able to see the output together only, becoz you will see output after execution of php code is completed only. You can execute this in terminal to see the out put when it happens.
Write this code into test.php
function curl_grab(){
echo "im called\n";
}
$arr = ['http://ab.com/','http://bc.com/'];
foreach($arr as $el){
curl_grab($el);
sleep(5);
}
To execute in terminal you can type
$ php /path/to/test.php
Related
I have a PHP script which runs a second script using proc_open().
The main script then passes the second script some data on STDIN.
I'd like the second script to be able to print output, and the main script to receive it.
So far that works fine.
However, my main script needs to call the second script many times, and to optimize this, I'd like to keep the resource open. That's ok too.
But I would see the output from the script progressively. That doesn't seem to be working: it looks like both scripts end up waiting for each other.
So what I want to do boils down to this:
// Main script
$resource = proc_open($command, $descriptorspec, $pipes);
foreach ($data as $line) {
fwrite($pipes[0], $line);
// Get output back from the second script's STDOUT to see how
// it reacted to this piece of data.
while ($line = fgets($this->pipes[1])) {
dump("script: $line");
}
}
// Second script.
while ($line = trim(fgets(STDIN))) {
print "some sort of output to report how I'm doing";
}
I've figured out a way for the second script to tell the main script to stop waiting:
print "\n";
and then the main script needs to do this when getting output from the process:
// Get output back from the second script's STDOUT to see how
// it reacted to this piece of data.
while ($line = trim(fgets($this->pipes[1]))) {
dump("script: $line");
}
The second script is basically using an empty line as a message to the main script to say 'I'm doing outputting for now, stop listening'.
I have a PHP file that currently runs via my console on my Mac. It's a private piece of software that interacts with an API via a loop that runs every x seconds.
At the minute Im am echoing out all information during each loop but the problem is within console it just repeats the content and appends it on to the previous. Is there a way of clearing all previous echoed results after each loop?
I thought I could use \r to replace the line but that doesn't seem to be working. I have also tried a few other examples after searching on here without any luck.
while(1){
echo "Some info goes here";
sleep(5);
}
For example if I try the following in a while loop
while(1){
echo "==============\r\n";
echo "==============\r\n";
sleep(5);
}
I end up with after a hand full of loops
Thanks
Two ways that I can think of off the top of my head:
Use the ANSI escape sequence to clear the screen:
print chr(27) . "[2J" . chr(27) . "[;H";
Run the command that clears the screen for your platform:
system("cls"); for windows
system("clear"); for unix-like systems
echo "\033[2J\033[;H";
Should clear your terminal.
If you just want to overwrite the same line again, this is working for me:
<?php
while(1) {
echo "test\r";
sleep(1);
}
?>
Let's say I have function that calls itself recursivly 3 times for example:
function recursion($n=1){
if($n<3){
echo 1;
recursion($n+1);
}
echo 2;
}
It must print 11222. But can I do something on last iteration to prevent previously iterations, to get 11? Im looking for any solution.
The reason your code is misbehaving is because your recursing() function is flawed.
Whenever you call a function, it gets placed on the stack. When the function finishes, it gets popped off the stack and returns to the caller.
In your example, recursion() checks to see if n < 3, then prints "1" and calls recursion() with n+1. However, the function doesn't stop there. It still runs!
After recursion() is being called with n = 3, the instruction pointer gets back to where the previous call to recursion() was being made, and continues. And what do you tell it to continue with? Well, printing "2" of course.
The Solution
function recursion($n=1){
if($n<3){
echo 1;
recursion($n+1);
}
else {
echo 2;
}
}
I am trainee, learning PHP. As a small project I wrote some code.
I am trying to make a (test) website about cars. I want to show that each car has several options. I have a database in MySQL.
I wrote (this is just a part of the code):
foreach ($key['options'] as $options) {
$contents = str_replace("[OPTION]",$options['text'], $contents);
}
$result= '';
foreach ($key['motor'] as $motor) {
$nm = $motor['name'];
$cnt = $motor['count'];
$result .= $cnt ." " .$nm. " ";
}
return $result;
$paginas .= $contents;
echo $paginas;
So far the code. The thing is, after the result code, the script stops (which is normal).
But I don't want that the script stops, the only thing I want is that the final echo also echo's all my return $result options PLUS the echo of '$paginas'.
It is a bit hard to explain maybe, but I hope you guys understand.
Have you read the documentation of the return statement?
return returns program control to the calling module. Execution resumes at the expression following the called module's invocation.
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.
If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file.
No matter where it is used, it transfers the control to a different part of the code. The statement(s) following the return statement in the current context are not executed.
I edited the code to echo instead of return. That works.
I made blocks on the website where the information about the car is shown. 1 block per car
The thing is with the $result echo: Now I do not get a list of all different options in 1 block, but I get for every option a new 'block'. so for example:
Block: Steeringwheel. Block:Headlights. Block: Engine type
My goal is to get all these things into 1 block.
I have two phps that do separate tasks and are executed using cronjobs. One executes every 5 mins and the other every 2 months. I want to make sure that when the 2 month php starts executing, the 5 min php isn't executing.
I want something like this,
PHP 1:
function execute1()
{
//set value of static variable
//do job
//unset value of static variable
}
PHP 2:
function execute2()
{
//This is the part where I need help
if (static_variable is set) //keep on waiting til value is unset
{
//do job
}
}
Should I use semaphores?