Jump to content

How do I make a loop for my simple PHP coding?


project168

Recommended Posts

How would I add a loop to this PHP coding? I want to make it so that after 'I AM THOMAS' it will loopback to 'HELLO WORLD' and repeat the same cycle infinitely.

 

How do I make a loop for PHP? Is this right?

 

<?PHP

require ("config.php");

exec("mode com3: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");

$fp =fopen("com3", "w");

while (true){

fwrite($fp,"HELLO WORLD");

fwrite($fp,"I AM THOMAS");

sleep(5);

}

fclose($fp);

?>

 

How would I add a loop to this PHP coding? I want to make it so that after 'I AM THOMAS' it will loopback to 'HELLO WORLD' and repeat the same cycle infinitely.

 

How do I make a loop for PHP? Is this right?

 

<?PHP

require ("config.php");

exec("mode com3: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");

$fp =fopen("com3", "w");

while (true){

fwrite($fp,"HELLO WORLD");

fwrite($fp,"I AM THOMAS");

sleep(5);

}

fclose($fp);

?>

 

 

$x = true;
while (true){
if ($x) fwrite($fp,"I AM THOMAS");
else fwrite($fp,"HELLO WORLD");
$x = !$x;
sleep(5);
}

No, that loop will write out "I AM THOMAS" & "HELLO WORLD" alternating. Use this instead

<?php
require ("config.php");
exec("mode com3: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
$fp =fopen("com3", "w");
fwrite($fp,"I AM THOMAS");
while (true) {
   fwrite($fp,"HELLO WORLD");
}
fclose($fp); // this line will never be executed
?>

 

This code will stop after 30 seconds, since that is the default maximum time a PHP script can run.

 

Ken

No, that loop will write out "I AM THOMAS" & "HELLO WORLD" alternating. Use this instead

<?php
require ("config.php");
exec("mode com3: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
$fp =fopen("com3", "w");
fwrite($fp,"I AM THOMAS");
while (true) {
   fwrite($fp,"HELLO WORLD");
}
fclose($fp); // this line will never be executed
?>

 

This code will stop after 30 seconds, since that is the default maximum time a PHP script can run.

 

Ken

 

hey Ken, that explains why my code stops after 30 seconds. Is there anyways to maximize the default time of PHP script which it can run up to maybe 5 mins?

 

Or atleast an alternative way to repeat the cycle of my fwrite? Is there some ways to be implement this without going to the max of 30s? Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.