project168 Posted March 27, 2011 Share Posted March 27, 2011 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 https://forums.phpfreaks.com/topic/231826-how-do-i-make-a-loop-for-my-simple-php-coding/ Share on other sites More sharing options...
RussellReal Posted March 27, 2011 Share Posted March 27, 2011 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 https://forums.phpfreaks.com/topic/231826-how-do-i-make-a-loop-for-my-simple-php-coding/#findComment-1192767 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2011 Share Posted March 27, 2011 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 https://forums.phpfreaks.com/topic/231826-how-do-i-make-a-loop-for-my-simple-php-coding/#findComment-1192769 Share on other sites More sharing options...
project168 Posted March 27, 2011 Author Share Posted March 27, 2011 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 https://forums.phpfreaks.com/topic/231826-how-do-i-make-a-loop-for-my-simple-php-coding/#findComment-1192933 Share on other sites More sharing options...
kenrbnsn Posted March 28, 2011 Share Posted March 28, 2011 What are you trying to do, exactly? To change the maximum execution time, use the function set_time_limit() Ken Link to comment https://forums.phpfreaks.com/topic/231826-how-do-i-make-a-loop-for-my-simple-php-coding/#findComment-1193139 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.