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); ?> Quote 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); } Quote 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 Quote 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 Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.