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);

?>

 

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.